forked from orbit-oss/flask
Merge pull request #2314 from cerickson/errorhandler
Added support for generic HTTPException handlers on app and blueprints
This commit is contained in:
commit
e206764955
2 changed files with 67 additions and 12 deletions
22
flask/app.py
22
flask/app.py
|
|
@ -1460,15 +1460,17 @@ class Flask(_PackageBoundObject):
|
|||
return f
|
||||
|
||||
def _find_error_handler(self, e):
|
||||
"""Finds a registered error handler for the request’s blueprint.
|
||||
Otherwise falls back to the app, returns None if not a suitable
|
||||
handler is found.
|
||||
"""Find a registered error handler for a request in this order:
|
||||
blueprint handler for a specific code, app handler for a specific code,
|
||||
blueprint generic HTTPException handler, app generic HTTPException handler,
|
||||
and returns None if a suitable handler is not found.
|
||||
"""
|
||||
exc_class, code = self._get_exc_class_and_code(type(e))
|
||||
|
||||
def find_handler(handler_map):
|
||||
if not handler_map:
|
||||
return
|
||||
|
||||
for cls in exc_class.__mro__:
|
||||
handler = handler_map.get(cls)
|
||||
if handler is not None:
|
||||
|
|
@ -1476,15 +1478,13 @@ class Flask(_PackageBoundObject):
|
|||
handler_map[exc_class] = handler
|
||||
return handler
|
||||
|
||||
# try blueprint handlers
|
||||
handler = find_handler(self.error_handler_spec
|
||||
.get(request.blueprint, {})
|
||||
.get(code))
|
||||
if handler is not None:
|
||||
return handler
|
||||
# check for any in blueprint or app
|
||||
for name, c in ((request.blueprint, code), (None, code),
|
||||
(request.blueprint, None), (None, None)):
|
||||
handler = find_handler(self.error_handler_spec.get(name, {}).get(c))
|
||||
|
||||
# fall back to app handlers
|
||||
return find_handler(self.error_handler_spec[None].get(code))
|
||||
if handler:
|
||||
return handler
|
||||
|
||||
def handle_http_exception(self, e):
|
||||
"""Handles an HTTP exception. By default this will invoke the
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue