Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add better error handling for invalid client URLs #388

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [[Unreleased]]
### Added:
- Support for dynamic fee calculation
- Better error handling for invalid client URL

### Fixed
- Resolve `txnNotFound` error with `send_reliable_submission` when waiting for a submitted malformed transaction
Expand Down
Empty file added tests/unit/clients/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions tests/unit/clients/test_json_rpc_client.py
@@ -0,0 +1,25 @@
"""Test the json_rpc_client."""
from __future__ import annotations

from unittest import TestCase

from xrpl.asyncio.clients.exceptions import XRPLRequestFailureException
from xrpl.clients import JsonRpcClient
from xrpl.models.requests import ServerInfo


class TestJsonRpcClient(TestCase):
"""Test json_rpc_client."""

def test_json_rpc_client_valid_url(self: TestJsonRpcClient) -> None:
# Valid URL
JSON_RPC_URL = "https://s.altnet.rippletest.net:51234/"
client = JsonRpcClient(JSON_RPC_URL)
client.request(ServerInfo())
khancode marked this conversation as resolved.
Show resolved Hide resolved

def test_json_rpc_client_invalid_url(self: TestJsonRpcClient) -> None:
# Invalid URL
JSON_RPC_URL = "https://s2.ripple.com:51233/"
with self.assertRaises(XRPLRequestFailureException):
client = JsonRpcClient(JSON_RPC_URL)
client.request(ServerInfo())
16 changes: 15 additions & 1 deletion xrpl/asyncio/clients/json_rpc_base.py
@@ -1,10 +1,13 @@
"""A common interface for JsonRpc requests."""
from __future__ import annotations

from json import JSONDecodeError

from httpx import AsyncClient
from typing_extensions import Final

from xrpl.asyncio.clients.client import Client
from xrpl.asyncio.clients.exceptions import XRPLRequestFailureException
from xrpl.asyncio.clients.utils import json_to_response, request_to_json_rpc
from xrpl.models.requests.request import Request
from xrpl.models.response import Response
Expand All @@ -29,11 +32,22 @@ async def request_impl(self: JsonRpcBase, request: Request) -> Response:
Returns:
The response from the server, as a Response object.

Raises:
XRPLRequestFailureException: if response can't be JSON decoded.

:meta private:
"""
async with AsyncClient(timeout=_TIMEOUT) as http_client:
response = await http_client.post(
self.url,
json=request_to_json_rpc(request),
)
return json_to_response(response.json())
try:
return json_to_response(response.json())
except JSONDecodeError:
raise XRPLRequestFailureException(
{
"error": response.status_code,
"error_message": response.text,
}
)