Skip to content
Myr

Register errorhandlers for Exceptions

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

Allow a default errorhandler by registering an errorhandler for HTTPException tests included

authored by Florian Sachs · committed by cerickson · parent c8e56d5 · Browse files · 2 files+34 added−2 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+34 added−2 removed

  • tests:+24 added−1 removed
  • other:+10 added−1 removed

2 files+34 added−2 removed

modified:flask/app.py+10 added-1 removed
1482 unmodified lines
1483 if handler is not None:1484 return handler14851486 # fall back to app handlers1487 return find_handler(self.error_handler_spec[None].get(code))1487 handler = find_handler(self.error_handler_spec[None].get(code))1488 if handler is not None:1489 return handler14901491 try:1492 handler = find_handler(self.error_handler_spec[None][None])1493 except KeyError:1494 handler = None14951496 return handler14971498 def handle_http_exception(self, e):1499 """Handles an HTTP exception. By default this will invoke the1500 registered error handlers and fall back to returning the1501 exception as response.
588 unmodified lines
modified:tests/test_user_error_handler.py+24 added-1 removed
1# -*- coding: utf-8 -*-2from werkzeug.exceptions import Forbidden, InternalServerError2from werkzeug.exceptions import Forbidden, InternalServerError, HTTPException, NotFound3import flask456def test_error_handler_no_match():
24 unmodified lines
31 assert c.get('/custom').data == b'custom'32 assert c.get('/keyerror').data == b'KeyError'333435def test_default_error_handler():36 app = flask.Flask(__name__)3738 @app.errorhandler(HTTPException)39 def catchall_errorhandler(e):40 assert isinstance(e, HTTPException)41 assert isinstance(e, NotFound)42 return 'default'4344 @app.errorhandler(Forbidden)45 def catchall_errorhandler(e):46 assert isinstance(e, Forbidden)47 return 'forbidden'4849 @app.route('/forbidden')50 def forbidden():51 raise Forbidden()5253 c = app.test_client()54 assert c.get('/undefined').data == b'default'55 assert c.get('/forbidden').data == b'forbidden'565758def test_error_handler_subclass():59 app = flask.Flask(__name__)6061 class ParentException(Exception):
102 unmodified lines