[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:
try:
d = await client.request(method, params=params)
except (rpc.RPCError, rpc.RPCTimeout):
except (rpc.RPCContentError, rpc.RPCTimeoutError):
# TODO: back off?
await asyncio.sleep(sleeptime)
continue
@ -101,7 +101,8 @@ def wallet_enabled(client):
async def check_getwalletinfo(client):
try:
await client.request("getwalletinfo")
except (rpc.RPCError, rpc.RPCTimeout):
except (rpc.RPCContentError, rpc.RPCTimeoutError):
# TODO: try again on timeout?
return False
try:

@ -81,19 +81,21 @@ def get_auth_from_datadir(datadir):
return craft_auth_from_credentials(rpcuser, rpcpassword)
class RPCError(BaseException):
""" Parent class for the RPC errors. """
pass
class RPCContentError(RPCError):
# TODO: include the error code, etc.
pass
class RPCTimeout(BaseException):
# TODO: include the error code, etc.
class RPCTimeoutError(RPCError):
# TODO: include the timeout value?
pass
class RPCConnectionError(BaseException):
# TODO: include the error code, etc.
class RPCConnectionError(RPCError):
pass
@ -126,7 +128,7 @@ class BitcoinRPCClient(object):
async with session.post(self._url, headers=self._headers, data=req) as response:
return await response.text()
except asyncio.TimeoutError:
raise RPCTimeout
raise RPCTimeoutError
except aiohttp.client_exceptions.ClientOSError:
raise RPCConnectionError
@ -143,19 +145,19 @@ class BitcoinRPCClient(object):
try:
error = d["error"]
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:
# 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:
result = d["result"]
except KeyError:
raise RPCError("RPC response seems malformed (no result field)")
raise RPCContentError("RPC response seems malformed (no result field)")
if result is 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

Loading…
Cancel
Save