Merge pull request #2314 from cerickson/errorhandler
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Added support for generic HTTPException handlers on app and blueprints
authored by David Lord · committed by GitHub · parents cd593bf, 361dba7 · Browse files · 2 files·+67 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+67 added−12 removed
- tests:+56 added−1 removed
- other:+11 added−11 removed
Provenance
- Commit
e206764- Tree
461ca01- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
cd593bf- Parent 2
361dba7
2 files changed+67 added−12 removed
modified:flask/app.py+11 added-11 removed
1458 unmodified lines1459 self.url_default_functions.setdefault(None, []).append(f)1460 return f1461 1462 def _find_error_handler(self, e):1463- """Finds a registered error handler for the request’s blueprint.1464- Otherwise falls back to the app, returns None if not a suitable1465- handler is found.1463+ """Find a registered error handler for a request in this order:1464+ blueprint handler for a specific code, app handler for a specific code,1465+ blueprint generic HTTPException handler, app generic HTTPException handler,1466+ and returns None if a suitable handler is not found.1467 """1468 exc_class, code = self._get_exc_class_and_code(type(e))1469 1470 def find_handler(handler_map):1471 if not handler_map:1472 return1473+1474 for cls in exc_class.__mro__:1475 handler = handler_map.get(cls)1476 if handler is not None:1477 # cache for next time exc_class is raised1478 handler_map[exc_class] = handler1479 return handler1480 1479- # try blueprint handlers1480- handler = find_handler(self.error_handler_spec1481- .get(request.blueprint, {})1482- .get(code))1483- if handler is not None:1484- return handler1481+ # check for any in blueprint or app1482+ for name, c in ((request.blueprint, code), (None, code),1483+ (request.blueprint, None), (None, None)):1484+ handler = find_handler(self.error_handler_spec.get(name, {}).get(c))1485 1486- # fall back to app handlers1487- return find_handler(self.error_handler_spec[None].get(code))1486+ if handler:1487+ return handler1488 1489 def handle_http_exception(self, e):1490 """Handles an HTTP exception. By default this will invoke the1491 registered error handlers and fall back to returning the589 unmodified linesmodified:tests/test_user_error_handler.py+56 added-1 removed
1 # -*- coding: utf-8 -*-2-from werkzeug.exceptions import Forbidden, InternalServerError2+from werkzeug.exceptions import (3+ Forbidden,4+ InternalServerError,5+ HTTPException,6+ NotFound7+ )8 import flask9 10 11 def test_error_handler_no_match():130 unmodified lines142 c = app.test_client()143 144 assert c.get('/error').data == b'app-error'145 assert c.get('/bp/error').data == b'bp-error'146+147+148+def test_default_error_handler():149+ bp = flask.Blueprint('bp', __name__)150+151+ @bp.errorhandler(HTTPException)152+ def bp_exception_handler(e):153+ assert isinstance(e, HTTPException)154+ assert isinstance(e, NotFound)155+ return 'bp-default'156+157+ @bp.errorhandler(Forbidden)158+ def bp_exception_handler(e):159+ assert isinstance(e, Forbidden)160+ return 'bp-forbidden'161+162+ @bp.route('/undefined')163+ def bp_registered_test():164+ raise NotFound()165+166+ @bp.route('/forbidden')167+ def bp_forbidden_test():168+ raise Forbidden()169+170+ app = flask.Flask(__name__)171+172+ @app.errorhandler(HTTPException)173+ def catchall_errorhandler(e):174+ assert isinstance(e, HTTPException)175+ assert isinstance(e, NotFound)176+ return 'default'177+178+ @app.errorhandler(Forbidden)179+ def catchall_errorhandler(e):180+ assert isinstance(e, Forbidden)181+ return 'forbidden'182+183+ @app.route('/forbidden')184+ def forbidden():185+ raise Forbidden()186+187+ app.register_blueprint(bp, url_prefix='/bp')188+189+ c = app.test_client()190+ assert c.get('/bp/undefined').data == b'bp-default'191+ assert c.get('/bp/forbidden').data == b'bp-forbidden'192+ assert c.get('/undefined').data == b'default'193+ assert c.get('/forbidden').data == b'forbidden'194+195+