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_jsonrpc_spec_v2_example8(prot):
try:
prot.parse_request("""[]""")
assert False
except JSONRPCInvalidRequestError as error:
e = error
response = e.error_respond()
assert _json_equal("""{"jsonrpc": "2.0", "error": {"code": -32600,
"message": "Invalid Request"}, "id": null}""",
response.serialize())
(JSONRPCInvalidRequestError, -32600, 'Invalid Request'),
(JSONRPCMethodNotFoundError, -32601, 'Method not found'),
(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)
def _get_code_message_and_data(error: Union[Exception, str]
) -> Tuple[int, str, Any]:
assert isinstance(error, (Exception, str))
data = None
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:
try:
req = json.loads(data)
except Exception as e:
raise JSONRPCParseError()
if isinstance(req, list):
# batch request
requests = JSONRPCBatchRequest()
for subreq in req:
try:
requests.append(self._parse_subrequest(subreq))
except RPCError as e:
requests.append(e)
except Exception as e:
requests.append(JSONRPCInvalidRequestError(request_id=subreq.get("id")))
if not requests:
raise JSONRPCInvalidRequestError()
return requests
else:
return self._parse_subrequest(req)
def _parse_subrequest(self, req):
if not isinstance(req, dict):
raise JSONRPCInvalidRequestError()
for k in req.keys():
if k not in self._ALLOWED_REQUEST_KEYS:
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
if req.get('jsonrpc', None) != self.JSON_RPC_VERSION:
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
if not isinstance(req['method'], str):
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
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"))
def _parse_subrequest(self, req):
if not isinstance(req, dict):
raise JSONRPCInvalidRequestError()
for k in req.keys():
if k not in self._ALLOWED_REQUEST_KEYS:
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
if req.get('jsonrpc', None) != self.JSON_RPC_VERSION:
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
if not isinstance(req['method'], str):
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
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:
def _get_code_message_and_data(error: Union[Exception, str]
) -> Tuple[int, str, Any]:
assert isinstance(error, (Exception, str))
data = None
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
def _parse_subrequest(self, req):
if not isinstance(req, dict):
raise JSONRPCInvalidRequestError()
for k in req.keys():
if k not in self._ALLOWED_REQUEST_KEYS:
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
if req.get('jsonrpc', None) != self.JSON_RPC_VERSION:
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
if not isinstance(req['method'], str):
raise JSONRPCInvalidRequestError(request_id=req.get("id"))
request = self.request_factory()
request.method = req['method']
request.one_way = 'id' not in req
if not request.one_way: