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_configuring_http_status(self):
NotificationResponse.http_status = status.HTTP_OK
req = Request({"jsonrpc": "2.0", "method": "foo"}).call([foo])
self.assertEqual(status.HTTP_OK, req.http_status)
NotificationResponse.http_status = status.HTTP_NO_CONTENT
def test_error_response_http_status():
response = ErrorResponse(
"foo", id=1, code=-1, http_status=status.HTTP_BAD_REQUEST, debug=False
)
assert response.http_status == status.HTTP_BAD_REQUEST
def test_client_json(self):
response = self.client.get('/non-existant', headers={'Accept': 'application/json-rpc'})
self.assert404(response)
self.assertIn(('Content-Type', 'application/json'), list(response.headers))
self.assertEqual(
{
'jsonrpc': '2.0',
'error': {
'code': status.JSONRPC_INVALID_REQUEST_CODE,
'message': status.JSONRPC_INVALID_REQUEST_TEXT,
'data': HTTP_STATUS_CODES[status.HTTP_404_NOT_FOUND]
},
'id': None
},
response.json
)
def __init__(
self, *args: Any, http_status: int = status.HTTP_BAD_REQUEST, **kwargs: Any
) -> None:
super().__init__(
"Invalid parameters",
code=status.JSONRPC_INVALID_PARAMS_CODE,
http_status=http_status,
*args,
**kwargs,
)
def response_to_json_query(response):
if check_error_response(response):
response: dict = response['error']
raise GenericJsonRpcServerError(
code=-int(response['code']),
message=response['message'],
http_status=status.HTTP_BAD_REQUEST
)
return response
def __init__(
self, *args: Any, http_status: int = status.HTTP_NOT_FOUND, **kwargs: Any
) -> None:
super().__init__(
"Method not found",
code=status.JSONRPC_METHOD_NOT_FOUND_CODE,
http_status=http_status,
*args,
**kwargs,
)
def __init__(
self,
exc: BaseException,
*args: Any,
http_status: int = status.HTTP_INTERNAL_ERROR,
**kwargs: Any,
) -> None:
super().__init__(
"Server error",
code=status.JSONRPC_SERVER_ERROR_CODE,
data=f"{exc.__class__.__name__}: {str(exc)}",
http_status=http_status,
*args,
**kwargs,
)
self.exc = exc
"""
message = None
def __init__(self, data=None):
super(JsonRpcServerError, self).__init__(self.message)
# Holds extra information related to the error.
self.data = data
class ParseError(JsonRpcServerError):
"""Raised when the request is not a valid JSON object."""
code = -32700
message = "Parse error"
http_status = status.HTTP_BAD_REQUEST
class InvalidRequest(JsonRpcServerError):
"""
Raised when the request is not a valid JSON-RPC object.
:param data: Extra information about the error that occurred (optional).
"""
code = -32600
message = "Invalid Request"
http_status = status.HTTP_BAD_REQUEST
class MethodNotFound(JsonRpcServerError):
"""
code=-int(response['code']),
message=response['message'],
http_status=status.HTTP_BAD_REQUEST
)
elif isinstance(response, dict):
tx_result = list(response.values())
tx_result = tx_result[0]
tx_hash = tx_result['txHash']
get_tx_result_mapper().put(tx_hash, tx_result)
return tx_hash
else:
raise GenericJsonRpcServerError(
code=-32603,
message="can't response_to_json_invoke_convert",
http_status=status.HTTP_BAD_REQUEST
)
def set_errorhandlers(dummy):
"""Set the errorhandlers for standard HTTP errors like 404. This is so we
can return the error in JSON-RPC format (if they've asked for json in the
Accept header)"""
# Override Flask's internal error handlers, to ensure we always return
# JSON-RPC
for code in default_exceptions.keys():
# Client errors (4xx) should respond with "Invalid request"
if status.is_http_client_error(code):
bp.app_errorhandler(code)(client_error)
# Everything else, respond with "Server error"
else:
bp.app_errorhandler(code)(server_error)