[rpc] Classify RPC errors and add a base class RPCError

master
Daniel Edgecumbe 7 years ago
parent f5be6b9cba
commit 7cf8496fc3

@ -56,7 +56,7 @@ async def poll_client(client, method, callback, sleeptime, params=None):
while True: while True:
try: try:
d = await client.request(method, params=params) d = await client.request(method, params=params)
except (rpc.RPCError, rpc.RPCTimeout): except (rpc.RPCContentError, rpc.RPCTimeoutError):
# TODO: back off? # TODO: back off?
await asyncio.sleep(sleeptime) await asyncio.sleep(sleeptime)
continue continue
@ -101,7 +101,8 @@ def wallet_enabled(client):
async def check_getwalletinfo(client): async def check_getwalletinfo(client):
try: try:
await client.request("getwalletinfo") await client.request("getwalletinfo")
except (rpc.RPCError, rpc.RPCTimeout): except (rpc.RPCContentError, rpc.RPCTimeoutError):
# TODO: try again on timeout?
return False return False
try: try:

@ -81,19 +81,21 @@ def get_auth_from_datadir(datadir):
return craft_auth_from_credentials(rpcuser, rpcpassword) return craft_auth_from_credentials(rpcuser, rpcpassword)
class RPCError(BaseException): class RPCError(BaseException):
""" Parent class for the RPC errors. """
pass
class RPCContentError(RPCError):
# TODO: include the error code, etc. # TODO: include the error code, etc.
pass pass
class RPCTimeout(BaseException): class RPCTimeoutError(RPCError):
# TODO: include the error code, etc. # TODO: include the timeout value?
pass pass
class RPCConnectionError(BaseException): class RPCConnectionError(RPCError):
# TODO: include the error code, etc.
pass pass
@ -126,7 +128,7 @@ class BitcoinRPCClient(object):
async with session.post(self._url, headers=self._headers, data=req) as response: async with session.post(self._url, headers=self._headers, data=req) as response:
return await response.text() return await response.text()
except asyncio.TimeoutError: except asyncio.TimeoutError:
raise RPCTimeout raise RPCTimeoutError
except aiohttp.client_exceptions.ClientOSError: except aiohttp.client_exceptions.ClientOSError:
raise RPCConnectionError raise RPCConnectionError
@ -143,19 +145,19 @@ class BitcoinRPCClient(object):
try: try:
error = d["error"] error = d["error"]
except KeyError: except KeyError:
raise RPCError("RPC response seems malformed (no error field)") raise RPCContentError("RPC response seems malformed (no error field)")
if error is not None: if error is not None:
# TODO: pass the error up the stack; tweak RPCError # TODO: pass the error up the stack; tweak RPCError
raise RPCError("RPC response returned error {}".format(error)) raise RPCContentError("RPC response returned error {}".format(error))
try: try:
result = d["result"] result = d["result"]
except KeyError: except KeyError:
raise RPCError("RPC response seems malformed (no result field)") raise RPCContentError("RPC response seems malformed (no result field)")
if result is None: if result is None:
# Is there a case in which a query can return None? # Is there a case in which a query can return None?
raise RPCError("RPC response returned a null result") raise RPCContentError("RPC response returned a null result")
return d return d

Loading…
Cancel
Save