Skip to content
Myr

Merge pull request #2362 from davidism/remove-error-handler-cache

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Remove error handler cache

authored by David Lord · committed by GitHub · parents 706e67e, b5f4c52 · Browse files · 3 files+48 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+48 added−15 removed

  • tests:+33 added−0 removed
  • other:+15 added−15 removed

3 files+48 added−15 removed

modified:flask/app.py+11 added-15 removed
1482 unmodified lines
1483 self.url_default_functions.setdefault(None, []).append(f)1484 return f14851486 def _find_error_handler(self, e):1487 """Find a registered error handler for a request in this order:1487 """Return a registered error handler for an exception in this order:1488 blueprint handler for a specific code, app handler for a specific code,1489 blueprint generic HTTPException handler, app generic HTTPException handler,1490 and returns None if a suitable handler is not found.1489 blueprint handler for an exception class, app handler for an exception1490 class, or ``None`` if a suitable handler is not found.1491 """1492 exc_class, code = self._get_exc_class_and_code(type(e))14931494 def find_handler(handler_map):1494 for name, c in (1495 (request.blueprint, code), (None, code),1496 (request.blueprint, None), (None, None)1497 ):1498 handler_map = self.error_handler_spec.setdefault(name, {}).get(c)14991500 if not handler_map:1496 return1501 continue15021503 for cls in exc_class.__mro__:1504 handler = handler_map.get(cls)15051506 if handler is not None:1501 # cache for next time exc_class is raised1502 handler_map[exc_class] = handler1507 return handler15081505 # check for any in blueprint or app1506 for name, c in ((request.blueprint, code), (None, code),1507 (request.blueprint, None), (None, None)):1508 handler = find_handler(self.error_handler_spec.get(name, {}).get(c))15091510 if handler:1511 return handler15121509 def handle_http_exception(self, e):1510 """Handles an HTTP exception. By default this will invoke the1511 registered error handlers and fall back to returning the1512 exception as response.
619 unmodified lines
modified:tests/test_basic.py+33 added
978 unmodified lines
979 assert client.get('/2').data == b'apple'980 assert client.get('/3').data == b'apple'981982983def test_errorhandler_precedence(app, client):984 class E1(Exception):985 pass986987 class E2(Exception):988 pass989990 class E3(E1, E2):991 pass992993 @app.errorhandler(E2)994 def handle_e2(e):995 return 'E2'996997 @app.errorhandler(Exception)998 def handle_exception(e):999 return 'Exception'10001001 @app.route('/E1')1002 def raise_e1():1003 raise E110041005 @app.route('/E3')1006 def raise_e3():1007 raise E310081009 rv = client.get('/E1')1010 assert rv.data == b'Exception'10111012 rv = client.get('/E3')1013 assert rv.data == b'E2'101410151016def test_trapping_of_bad_request_key_errors(app, client):1017 @app.route('/fail')1018 def fail():1019 flask.request.form['missing_key']
846 unmodified lines
modified:CHANGES+4 added
74 unmodified lines
75 (`#2358`_)76- Extract JSON handling to a mixin applied to both the request and response77 classes used by Flask. This adds the ``is_json`` and ``get_json`` methods to78 the response to make testing JSON response much easier. (`#2358`_)79- Removed error handler caching because it caused unexpected results for some80 exception inheritance hierarchies. Register handlers explicitly for each81 exception if you don't want to traverse the MRO. (`#2362`_)8283.. _#1489: https://github.com/pallets/flask/pull/148984.. _#1621: https://github.com/pallets/flask/pull/162185.. _#1898: https://github.com/pallets/flask/pull/1898
14 unmodified lines
100.. _#2348: https://github.com/pallets/flask/pull/2348101.. _#2352: https://github.com/pallets/flask/pull/2352102.. _#2354: https://github.com/pallets/flask/pull/2354103.. _#2358: https://github.com/pallets/flask/pull/2358104.. _#2362: https://github.com/pallets/flask/pull/2362105106Version 0.12.2107--------------108
617 unmodified lines