Skip to content
Myr

Check error handlers for specific classes first

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

This allows adding error handlers like this: @app.errorhandler(werkzeug.exceptions.Forbidden) And subclassing HTTPExceptions: class ForbiddenBecauseReason(Forbidden): pass @app.errorhandler(ForbiddenBecauseReason) def error1(): return "Forbidden because reason", 403 @app.errorhandler(403) def error2(): return "Forbidden", 403 ... the idea being, that a flask extension might want to raise an exception, with the default behaviour of creating a HTTP error page, but still allowing the user to add a view/handler specific to that exception (e.g., "Forbidden because you are not in the right group").

authored by Daniel Richman · committed by Daniel Richman · parent 2d8a21c · Browse files · 3 files+49 added−3 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+49 added−3 removed

  • other:+49 added−3 removed

3 files+49 added−3 removed

modified:flask/testsuite/basic.py+40 added-1 removed
16 unmodified lines
17from datetime import datetime18from threading import Thread19from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning20from flask._compat import text_type21from werkzeug.exceptions import BadRequest, NotFound21from werkzeug.exceptions import BadRequest, NotFound, Forbidden22from werkzeug.http import parse_date23from werkzeug.routing import BuildError2425
599 unmodified lines
625 return 'not found', 404626 @app.errorhandler(500)627 def internal_server_error(e):628 return 'internal server error', 500629 @app.errorhandler(Forbidden)630 def forbidden(e):631 return 'forbidden', 403632 @app.route('/')633 def index():634 flask.abort(404)635 @app.route('/error')636 def error():637 1 // 0638 @app.route('/forbidden')639 def error2():640 flask.abort(403)641 c = app.test_client()642 rv = c.get('/')643 self.assert_equal(rv.status_code, 404)644 self.assert_equal(rv.data, b'not found')645 rv = c.get('/error')646 self.assert_equal(rv.status_code, 500)647 self.assert_equal(b'internal server error', rv.data)648 rv = c.get('/forbidden')649 self.assert_equal(rv.status_code, 403)650 self.assert_equal(b'forbidden', rv.data)651652 def test_before_request_and_routing_errors(self):653 app = flask.Flask(__name__)654 @app.before_request
21 unmodified lines
676677 c = app.test_client()678 self.assert_equal(c.get('/').data, b'42')679680 def test_http_error_subclass_handling(self):681 class ForbiddenSubclass(Forbidden):682 pass683684 app = flask.Flask(__name__)685 @app.errorhandler(ForbiddenSubclass)686 def handle_forbidden_subclass(e):687 self.assert_true(isinstance(e, ForbiddenSubclass))688 return 'banana'689 @app.errorhandler(403)690 def handle_forbidden_subclass(e):691 self.assert_false(isinstance(e, ForbiddenSubclass))692 self.assert_true(isinstance(e, Forbidden))693 return 'apple'694695 @app.route('/1')696 def index1():697 raise ForbiddenSubclass()698 @app.route('/2')699 def index2():700 flask.abort(403)701 @app.route('/3')702 def index3():703 raise Forbidden()704705 c = app.test_client()706 self.assert_equal(c.get('/1').data, b'banana')707 self.assert_equal(c.get('/2').data, b'apple')708 self.assert_equal(c.get('/3').data, b'apple')709710 def test_trapping_of_bad_request_key_errors(self):711 app = flask.Flask(__name__)712 app.testing = True713 @app.route('/fail')
630 unmodified lines
modified:flask/app.py+3 added-2 removed
1363 unmodified lines
1364 # ensure not to trash sys.exc_info() at that point in case someone1365 # wants the traceback preserved in handle_http_exception. Of course1366 # we cannot prevent users from trashing it themselves in a custom1367 # trap_http_exception method so that's their fault then.1368 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1369 return self.handle_http_exception(e)13681369 blueprint_handlers = ()1370 handlers = self.error_handler_spec.get(request.blueprint)1371 if handlers is not None:
2 unmodified lines
1374 for typecheck, handler in chain(blueprint_handlers, app_handlers):1375 if isinstance(e, typecheck):1376 return handler(e)13771378 if isinstance(e, HTTPException) and not self.trap_http_exception(e):1379 return self.handle_http_exception(e)13801381 reraise(exc_type, exc_value, tb)13821383 def handle_exception(self, e):1384 """Default exception handling that kicks in when an exception
458 unmodified lines
modified:CHANGES+6 added
12 unmodified lines
13 refreshed each request and get their lifetime extended, if set to14 `False` it will only be modified if the session actually modifies.15 Non permanent sessions are not affected by this and will always16 expire if the browser window closes.17- Error handlers that match specific classes are now checked first,18 thereby allowing catching exceptions that are subclasses of HTTP19 exceptions (in ``werkzeug.execptions``). This makes it possible20 for an extension author to create exceptions that will by default21 result in the HTTP error of their choosing, but may be caught with22 a custom error handler if desired.2324Version 0.10.225--------------26
467 unmodified lines