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

[Proposal] Allow for registration of custom error handlers #2056

Open
cloutierMat opened this issue Jul 16, 2023 · 0 comments
Open

[Proposal] Allow for registration of custom error handlers #2056

cloutierMat opened this issue Jul 16, 2023 · 0 comments

Comments

@cloutierMat
Copy link

I first put my proposal in response to #1884. But in an effort to make sure the work I do is in a desired direction, I am copying my comments here so that it can be seen and hopefully handled.

Problem

While the ChaliceUnhandledError works fine for custom errors, third party libraries can raise Exceptions that can't be subclassed. Having a try/except in a middleware won't successfully catch the Exception as it is already a Response at that point.

Goals

  • Exceptions on REST APIs must be intercepted before the InternalServerError response is built.
  • Must have knowledge of the context of the raised exception.
  • Must be able to return a Response
  • An invalid Response or an exception raised in the handler should still return the standard InternalServerError and have the stack trace replaced by the expected json object when not in debug mode
  • As existing middlewares can already successfully intercept all exceptions for other events, and ChaliceUnhandledError only exists in the scope of http event, so will the focus of this handler.

Specification

A new @app.errorhandler is to be added. The decorated function will take one arg, the exception.

Similar to the way middlewares can be registered, we will be able to register the error handler in app or in a blueprint with either a decorator or app.register_error.

# Decorator
@app.errorhandler(MyCustomError)
def my_handler(e: MyCustomError):
    return Response(body="", status_code:400)

# Register
app.register_error_handler(MyCustomError, my_handler)

# Blueprint
@my_blueprint.errorhandler(MyCustomError)
def my_handler(e: MyCustomError):
    return Response(body="", status_code:400)
In the examples above, any http endpoint raising MyCustomError will then return a 400 with no body.

If, however, the return value isn't a Response or an exception was raised during execution of the handler, the normal flow of the RestAPIEventHandler will be maintained.

@app.errorhandler(MyCustomError)
def my_handler(e: MyCustomError):
    return "Not a Response object"

Will return

{
    "Code": "InternalServerError",
    "Message": "An internal server error occurred."
}
@cloutierMat cloutierMat changed the title Allow for registration of custom error handlers [Proposal] Allow for registration of custom error handlers Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant