Improve electrumx_rpc error handling

This commit is contained in:
Neil Booth 2018-08-06 18:35:19 +09:00
parent c141bfffd9
commit c9f97d98e0

View File

@ -10,7 +10,7 @@
'''Script to send RPC commands to a running ElectrumX server.''' '''Script to send RPC commands to a running ElectrumX server.'''
from aiorpcx import timeout_after from aiorpcx import timeout_after, ClientSession, TaskTimeout
import argparse import argparse
import asyncio import asyncio
import json import json
@ -18,7 +18,6 @@ from os import environ
import electrumx.lib.text as text import electrumx.lib.text as text
from aiorpcx import ClientSession
simple_commands = { simple_commands = {
'getinfo': 'Print a summary of server state', 'getinfo': 'Print a summary of server state',
@ -113,7 +112,8 @@ def main():
# aiorpcX makes this so easy... # aiorpcX makes this so easy...
async def send_request(): async def send_request():
async with timeout_after(15): try:
async with timeout_after(1):
async with ClientSession('localhost', port) as session: async with ClientSession('localhost', port) as session:
result = await session.send_request(method, args) result = await session.send_request(method, args)
if method in ('query', ): if method in ('query', ):
@ -125,15 +125,16 @@ def main():
print(line) print(line)
else: else:
print(json.dumps(result, indent=4, sort_keys=True)) print(json.dumps(result, indent=4, sort_keys=True))
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(send_request())
except OSError: except OSError:
print('cannot connect - is ElectrumX catching up, not running, or ' print('cannot connect - is ElectrumX catching up, not running, or '
f'is {port} the wrong RPC port?') f'is {port} the wrong RPC port?')
except TaskTimeout as e:
print(f'request timed out after {e.args[0]}s')
except Exception as e: except Exception as e:
print(f'error making request: {e}') print(f'error making request: {e!r}')
loop = asyncio.get_event_loop()
loop.run_until_complete(send_request())
if __name__ == '__main__': if __name__ == '__main__':