Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parsing_invalid_arguments(prot):
with pytest.raises(JSONRPCInvalidParamsError):
prot.parse_request(
"""{"jsonrpc": "2.0", "method": "update", "params": 9}"""
)
(JSONRPCInvalidParamsError, -32602, 'Invalid params'),
(JSONRPCInternalError, -32603, 'Internal error'),
# generic errors
#(InvalidRequestError, -32600, 'Invalid Request'),
#(MethodNotFoundError, -32601, 'Method not found'),
#(ServerError, -32603, 'Internal error'),
])
def test_proper_construction_of_error_codes(prot, exc, code, message):
request = prot.parse_request(
"""{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4],
"id": "1"}"""
)
reply = exc().error_respond().serialize()
assert isinstance(reply, bytes)
reply = reply.decode()
request = self.request_factory()
request.method = req['method']
request.one_way = 'id' not in req
if not request.one_way:
request.unique_id = req['id']
params = req.get('params', None)
if params is not None:
if isinstance(params, list):
request.args = req['params']
elif isinstance(params, dict):
request.kwargs = req['params']
else:
raise JSONRPCInvalidParamsError(request_id=req.get("id"))
return request
if isinstance(error, Exception):
if hasattr(error, 'jsonrpc_error_code'):
code = error.jsonrpc_error_code
msg = str(error)
try:
data = error.data
except AttributeError:
pass
elif isinstance(error, InvalidRequestError):
code = JSONRPCInvalidRequestError.jsonrpc_error_code
msg = JSONRPCInvalidRequestError.message
elif isinstance(error, MethodNotFoundError):
code = JSONRPCMethodNotFoundError.jsonrpc_error_code
msg = JSONRPCMethodNotFoundError.message
elif isinstance(error, InvalidParamsError):
code = JSONRPCInvalidParamsError.jsonrpc_error_code
msg = JSONRPCInvalidParamsError.message
else:
# allow exception message to propagate
code = JSONRPCServerError.jsonrpc_error_code
if len(error.args) == 2:
msg = str(error.args[0])
data = error.args[1]
else:
msg = str(error)
else:
code = -32000
msg = error
return code, msg, data
if hasattr(error, 'jsonrpc_error_code'):
code = error.jsonrpc_error_code
msg = str(error)
try:
data = error.data
except AttributeError:
pass
elif isinstance(error, InvalidRequestError):
code = JSONRPCInvalidRequestError.jsonrpc_error_code
msg = JSONRPCInvalidRequestError.message
elif isinstance(error, MethodNotFoundError):
code = JSONRPCMethodNotFoundError.jsonrpc_error_code
msg = JSONRPCMethodNotFoundError.message
elif isinstance(error, InvalidParamsError):
code = JSONRPCInvalidParamsError.jsonrpc_error_code
msg = JSONRPCInvalidParamsError.message
else:
# allow exception message to propagate
code = JSONRPCServerError.jsonrpc_error_code
if len(error.args) == 2:
msg = str(error.args[0])
data = error.args[1]
else:
msg = str(error)
else:
code = -32000
msg = error
return code, msg, data