Improve electrumx_rpc error handling
This commit is contained in:
parent
c141bfffd9
commit
c9f97d98e0
@ -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,27 +112,29 @@ 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 ClientSession('localhost', port) as session:
|
async with timeout_after(1):
|
||||||
result = await session.send_request(method, args)
|
async with ClientSession('localhost', port) as session:
|
||||||
if method in ('query', ):
|
result = await session.send_request(method, args)
|
||||||
for line in result:
|
if method in ('query', ):
|
||||||
print(line)
|
for line in result:
|
||||||
elif method in ('groups', 'peers', 'sessions'):
|
print(line)
|
||||||
lines_func = getattr(text, f'{method}_lines')
|
elif method in ('groups', 'peers', 'sessions'):
|
||||||
for line in lines_func(result):
|
lines_func = getattr(text, f'{method}_lines')
|
||||||
print(line)
|
for line in lines_func(result):
|
||||||
else:
|
print(line)
|
||||||
print(json.dumps(result, indent=4, sort_keys=True))
|
else:
|
||||||
|
print(json.dumps(result, indent=4, sort_keys=True))
|
||||||
|
except OSError:
|
||||||
|
print('cannot connect - is ElectrumX catching up, not running, or '
|
||||||
|
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:
|
||||||
|
print(f'error making request: {e!r}')
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
try:
|
loop.run_until_complete(send_request())
|
||||||
loop.run_until_complete(send_request())
|
|
||||||
except OSError:
|
|
||||||
print('cannot connect - is ElectrumX catching up, not running, or '
|
|
||||||
f'is {port} the wrong RPC port?')
|
|
||||||
except Exception as e:
|
|
||||||
print(f'error making request: {e}')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user