diff --git a/pyhmy/account.py b/pyhmy/account.py index 294660e..6e36068 100644 --- a/pyhmy/account.py +++ b/pyhmy/account.py @@ -6,8 +6,7 @@ from .rpc.exceptions import ( RPCError, InvalidRPCReplyError, RequestsError, - RequestsTimeoutError, - JSONDecodeError + RequestsTimeoutError ) from .blockchain import ( @@ -303,7 +302,7 @@ def get_balance_on_all_shards(address, skip_error=True, endpoint=_default_endpoi 'shard': shard['shardID'], 'balance': get_balance(address, endpoint=shard['http'], timeout=timeout) }) - except (KeyError, RPCError, RequestsError, RequestsTimeoutError, JSONDecodeError): + except (KeyError, RPCError, RequestsError, RequestsTimeoutError): if not skip_error: balances.append({ 'shard': shard['shardID'], diff --git a/pyhmy/rpc/exceptions.py b/pyhmy/rpc/exceptions.py index 2db62b9..0f12d8c 100644 --- a/pyhmy/rpc/exceptions.py +++ b/pyhmy/rpc/exceptions.py @@ -7,6 +7,9 @@ class RPCError(RuntimeError): Exception raised when RPC call returns an error """ + def __init__(self, method, endpoint, error): + super().__init__(f'Error in reply from {endpoint}: {method} returned {error}') + class InvalidRPCReplyError(RuntimeError): """ Exception raised when RPC call returns unexpected result @@ -14,19 +17,20 @@ class InvalidRPCReplyError(RuntimeError): """ def __init__(self, method, endpoint): - self.message = f'Unexpected reply for {method} from {endpoint}' - -class JSONDecodeError(json.decoder.JSONDecodeError): - """ - Wrapper for json lib DecodeError exception - """ + super().__init__(f'Unexpected reply for {method} from {endpoint}') class RequestsError(requests.exceptions.RequestException): """ Wrapper for requests lib exceptions """ + def __init__(self, endpoint): + super().__init__(f'Error connecting to {endpoint}') + class RequestsTimeoutError(requests.exceptions.Timeout): """ Wrapper for requests lib Timeout exceptions """ + + def __init__(self, endpoint): + super().__init__(f'Error connecting to {endpoint}') diff --git a/pyhmy/rpc/request.py b/pyhmy/rpc/request.py index fc0f2c7..9eb76be 100644 --- a/pyhmy/rpc/request.py +++ b/pyhmy/rpc/request.py @@ -3,7 +3,6 @@ import json import requests from .exceptions import ( - JSONDecodeError, RequestsError, RequestsTimeoutError, RPCError @@ -63,9 +62,9 @@ def base_request(method, params=None, endpoint=_default_endpoint, timeout=_defau timeout=timeout, allow_redirects=True) return resp.content except requests.exceptions.Timeout as err: - raise RequestsTimeoutError() from err + raise RequestsTimeoutError(endpoint) from err except requests.exceptions.RequestException as err: - raise RequestsError() from err + raise RequestsError(endpoint) from err def rpc_request(method, params=None, endpoint=_default_endpoint, timeout=_default_timeout) -> dict: @@ -110,10 +109,10 @@ def rpc_request(method, params=None, endpoint=_default_endpoint, timeout=_defaul try: resp = json.loads(raw_resp) if 'error' in resp: - raise RPCError(str(resp['error'])) + raise RPCError(method, endpoint, str(resp['error'])) return resp except json.decoder.JSONDecodeError as err: - raise JSONDecodeError() from err + raise RPCError(method, endpoint, raw_resp) from err # TODO: Add GET requests diff --git a/pyhmy/util.py b/pyhmy/util.py index 41beb92..e9c4960 100644 --- a/pyhmy/util.py +++ b/pyhmy/util.py @@ -12,7 +12,6 @@ from .blockchain import ( from .rpc.exceptions import ( RPCError, - JSONDecodeError, RequestsError, RequestsTimeoutError, ) @@ -63,7 +62,7 @@ def is_active_shard(endpoint, delay_tolerance=60): timestamp = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f").replace(tzinfo=None) time_delta = curr_time - timestamp return abs(time_delta.seconds) < delay_tolerance - except (RPCError, JSONDecodeError, RequestsError, RequestsTimeoutError): + except (RPCError, RequestsError, RequestsTimeoutError): return False