[exceptions] Remove JSONDecodeError wrapper, include in RPCError (#5)

pull/6/head
Janet Liang 5 years ago committed by GitHub
parent b1118b7030
commit 27cdf2cb7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      pyhmy/account.py
  2. 16
      pyhmy/rpc/exceptions.py
  3. 9
      pyhmy/rpc/request.py
  4. 3
      pyhmy/util.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'],

@ -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}')

@ -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

@ -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

Loading…
Cancel
Save