Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

werkzeug.exceptions.BadRequest (400) is converted to internal server error (500) #289

Closed
Talkless opened this issue Sep 6, 2022 · 1 comment
Assignees
Labels

Comments

@Talkless
Copy link

Talkless commented Sep 6, 2022

If we have:

@jsonrpc.method('MyApp.index')
@jwt_required()
def index(name: str) -> str:
    raise werkzeug.exceptions.BadRequest("bad parameter")

In result we get (from console output of Flask server):

<..backtrace..>
werkzeug.exceptions.BadRequest: 400 Bad Request: bad parameter
127.0.0.1 - - [06/Sep/2022 15:48:52] "POST /myapi HTTP/1.1" 500 -

requests.post() and then .raise_for_status() results in:

requests.exceptions.HTTPError: 500 Server Error: INTERNAL SERVER ERROR for url: http://127.0.0.1:5000/myapi

It would be nice to be able to validate parameters ourselves, and respond with appropriate error codes and messages to the API user.

@nycholas nycholas self-assigned this Sep 12, 2022
@nycholas
Copy link
Member

Hi @Talkless,

Yes, according to JSON-RPC specification, every code errors are here[1], and there is a range of custom errors:

-32000 to -32099 | Server error | Reserved for implementation-defined server-errors.

You can use the JSONRPCError[2] to do it, for example:

error = JSONRPCError(message="I'm a teapot", code=-32768, data={'data': [1, 2, 3]}, status_code=418)
assert error.code == -32768
assert error.message == "I'm a teapot"
assert error.data == {'data': [1, 2, 3]}
assert error.status_code == 418
assert error.jsonrpc_format == {
        'code': -32768,
        'data': {'data': [1, 2, 3]},
        'message': "I'm a teapot",
        'name': 'JSONRPCError',
}

[1] - https://www.jsonrpc.org/specification#error_object
[2] - https://github.com/cenobites/flask-jsonrpc/blob/master/src/flask_jsonrpc/exceptions.py#L41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants