Skip to content

Commit

Permalink
add better error handling for invalid client URLs (#388)
Browse files Browse the repository at this point in the history
* add better error handling for invalid client URLs
  • Loading branch information
khancode committed May 13, 2022
1 parent 4e78156 commit 5acfc99
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
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.
19 changes: 19 additions & 0 deletions tests/unit/clients/test_json_rpc_client.py
@@ -0,0 +1,19 @@
"""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_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,
}
)

0 comments on commit 5acfc99

Please sign in to comment.