cenobites / flask-jsonrpc

Basic JSON-RPC implementation for your Flask-powered sites
BSD 3-Clause "New" or "Revised" License
289 stars 63 forks source link

Allow to use custom error codes for unexpected errors #375

Closed Talkless closed 1 month ago

Talkless commented 1 year ago

Any unexpected error is translated to ServerError with default error code: https://github.com/cenobites/flask-jsonrpc/blob/a72a0c4ecaf4adce8086ea277161c35482e7ddcc/src/flask_jsonrpc/site.py#L146

It would be useful to convert, for example, JWTError (https://github.com/mattupstate/flask-jwt/blob/c27084114e258863b82753fc574a362cd6c62fcd/flask_jwt/__init__.py#L160) to ServerError with specific error code for easier and more deterministic (avoiding string comparison) error handling in frontend.

Changing

jsonrpc_error = ServerError(data={'message': str(e)})

into something similar to:

jsonrpc_error = self.make_unexpected_error(e)

could maybe work, if we then could specify custom handler for unexpected errors.

nycholas commented 1 year ago

Maybe here, instead of creating an error handling by scratch, uses the Flask Error Handling: https://flask.palletsprojects.com/en/2.2.x/errorhandling/#error-handlers

I will check out how to use that, ;).

Talkless commented 1 month ago

Great, thanks @nycholas !