Skip to content
Myr

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

2 files+67 added−12 removed

modified:flask/app.py+11 added-11 removed
1458 unmodified lines
1459 self.url_default_functions.setdefault(None, []).append(f)1460 return f14611462 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))14691470 def find_handler(handler_map):1471 if not handler_map:1472 return14731474 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 handler14801479 # 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))14851486 # fall back to app handlers1487 return find_handler(self.error_handler_spec[None].get(code))1486 if handler:1487 return handler14881489 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 the
589 unmodified lines
modified:tests/test_user_error_handler.py+56 added-1 removed
1# -*- coding: utf-8 -*-2from werkzeug.exceptions import Forbidden, InternalServerError2from werkzeug.exceptions import (3 Forbidden,4 InternalServerError,5 HTTPException,6 NotFound7 )8import flask91011def test_error_handler_no_match():
130 unmodified lines
142 c = app.test_client()143144 assert c.get('/error').data == b'app-error'145 assert c.get('/bp/error').data == b'bp-error'146147148def test_default_error_handler():149 bp = flask.Blueprint('bp', __name__)150151 @bp.errorhandler(HTTPException)152 def bp_exception_handler(e):153 assert isinstance(e, HTTPException)154 assert isinstance(e, NotFound)155 return 'bp-default'156157 @bp.errorhandler(Forbidden)158 def bp_exception_handler(e):159 assert isinstance(e, Forbidden)160 return 'bp-forbidden'161162 @bp.route('/undefined')163 def bp_registered_test():164 raise NotFound()165166 @bp.route('/forbidden')167 def bp_forbidden_test():168 raise Forbidden()169170 app = flask.Flask(__name__)171172 @app.errorhandler(HTTPException)173 def catchall_errorhandler(e):174 assert isinstance(e, HTTPException)175 assert isinstance(e, NotFound)176 return 'default'177178 @app.errorhandler(Forbidden)179 def catchall_errorhandler(e):180 assert isinstance(e, Forbidden)181 return 'forbidden'182183 @app.route('/forbidden')184 def forbidden():185 raise Forbidden()186187 app.register_blueprint(bp, url_prefix='/bp')188189 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'194195