forked from LBRYCommunity/lbry-sdk
fixed error handling when calling commands
This commit is contained in:
parent
a38643cca6
commit
69ccbda168
1 changed files with 29 additions and 12 deletions
|
@ -8,6 +8,7 @@ from operator import itemgetter
|
||||||
from binascii import hexlify, unhexlify
|
from binascii import hexlify, unhexlify
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from twisted.internet.task import LoopingCall
|
from twisted.internet.task import LoopingCall
|
||||||
|
from traceback import format_exc
|
||||||
|
|
||||||
from torba.client.baseaccount import SingleKey, HierarchicalDeterministic
|
from torba.client.baseaccount import SingleKey, HierarchicalDeterministic
|
||||||
|
|
||||||
|
@ -469,17 +470,28 @@ class Daemon(metaclass=JSONRPCServerType):
|
||||||
|
|
||||||
async def handle_old_jsonrpc(self, request):
|
async def handle_old_jsonrpc(self, request):
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
|
result = await self._process_rpc_call(data)
|
||||||
|
return web.Response(
|
||||||
|
text=jsonrpc_dumps_pretty(result, ledger=self.ledger),
|
||||||
|
content_type='application/json'
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _process_rpc_call(self, data):
|
||||||
args = data.get('params', {})
|
args = data.get('params', {})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
function_name = data['method']
|
function_name = data['method']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise web.HTTPBadRequest(text="Missing 'method' value in request.")
|
return JSONRPCError(
|
||||||
|
"Missing 'method' value in request.", JSONRPCError.CODE_METHOD_NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fn = self._get_jsonrpc_method(function_name)
|
fn = self._get_jsonrpc_method(function_name)
|
||||||
except UnknownAPIMethodError:
|
except UnknownAPIMethodError:
|
||||||
raise web.HTTPBadRequest(text=f"Invalid method requested: {function_name}.")
|
return JSONRPCError(
|
||||||
|
f"Invalid method requested: {function_name}.", JSONRPCError.CODE_METHOD_NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
if args in (EMPTY_PARAMS, []):
|
if args in (EMPTY_PARAMS, []):
|
||||||
_args, _kwargs = (), {}
|
_args, _kwargs = (), {}
|
||||||
|
@ -492,7 +504,9 @@ class Daemon(metaclass=JSONRPCServerType):
|
||||||
elif len(args) == 2 and isinstance(args[0], list) and isinstance(args[1], dict):
|
elif len(args) == 2 and isinstance(args[0], list) and isinstance(args[1], dict):
|
||||||
_args, _kwargs = args
|
_args, _kwargs = args
|
||||||
else:
|
else:
|
||||||
raise web.HTTPBadRequest(text="invalid args format")
|
return JSONRPCError(
|
||||||
|
f"Invalid parameters format.", JSONRPCError.CODE_INVALID_PARAMS
|
||||||
|
)
|
||||||
|
|
||||||
params_error, erroneous_params = self._check_params(fn, _args, _kwargs)
|
params_error, erroneous_params = self._check_params(fn, _args, _kwargs)
|
||||||
if params_error is not None:
|
if params_error is not None:
|
||||||
|
@ -500,16 +514,19 @@ class Daemon(metaclass=JSONRPCServerType):
|
||||||
params_error, function_name, ', '.join(erroneous_params)
|
params_error, function_name, ', '.join(erroneous_params)
|
||||||
)
|
)
|
||||||
log.warning(params_error_message)
|
log.warning(params_error_message)
|
||||||
raise web.HTTPBadRequest(text=params_error_message)
|
return JSONRPCError(
|
||||||
|
params_error_message, JSONRPCError.CODE_INVALID_PARAMS
|
||||||
|
)
|
||||||
|
|
||||||
result = fn(self, *_args, **_kwargs)
|
try:
|
||||||
if asyncio.iscoroutine(result):
|
result = fn(self, *_args, **_kwargs)
|
||||||
result = await result
|
if asyncio.iscoroutine(result):
|
||||||
|
result = await result
|
||||||
return web.Response(
|
return result
|
||||||
text=jsonrpc_dumps_pretty(result, ledger=self.ledger),
|
except Exception as e: # pylint: disable=broad-except
|
||||||
content_type='application/json'
|
return JSONRPCError(
|
||||||
)
|
str(e), JSONRPCError.CODE_APPLICATION_ERROR, format_exc()
|
||||||
|
)
|
||||||
|
|
||||||
def _verify_method_is_callable(self, function_path):
|
def _verify_method_is_callable(self, function_path):
|
||||||
if function_path not in self.callable_methods:
|
if function_path not in self.callable_methods:
|
||||||
|
|
Loading…
Add table
Reference in a new issue