Check error handlers for specific classes first

This allows adding error handlers like this:

    @app.errorhandler(werkzeug.exceptions.Forbidden)

And subclassing HTTPExceptions:

    class ForbiddenBecauseReason(Forbidden): pass

    @app.errorhandler(ForbiddenBecauseReason)
    def error1(): return "Forbidden because reason", 403
    @app.errorhandler(403)
    def error2(): return "Forbidden", 403

... the idea being, that a flask extension might want to raise an
exception, with the default behaviour of creating a HTTP error page,
but still allowing the user to add a view/handler specific to that
exception (e.g., "Forbidden because you are not in the right group").
This commit is contained in:
Daniel Richman 2013-08-17 22:40:06 +00:00
parent 2d8a21c732
commit 3d67736e09
3 changed files with 49 additions and 3 deletions

View file

@ -1365,8 +1365,6 @@ class Flask(_PackageBoundObject):
# wants the traceback preserved in handle_http_exception. Of course
# we cannot prevent users from trashing it themselves in a custom
# trap_http_exception method so that's their fault then.
if isinstance(e, HTTPException) and not self.trap_http_exception(e):
return self.handle_http_exception(e)
blueprint_handlers = ()
handlers = self.error_handler_spec.get(request.blueprint)
@ -1377,6 +1375,9 @@ class Flask(_PackageBoundObject):
if isinstance(e, typecheck):
return handler(e)
if isinstance(e, HTTPException) and not self.trap_http_exception(e):
return self.handle_http_exception(e)
reraise(exc_type, exc_value, tb)
def handle_exception(self, e):