Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #941 -- allow errorhandlers for HTTPException #952

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions flask/app.py
Expand Up @@ -1360,13 +1360,6 @@ def handle_user_exception(self, e):
exc_type, exc_value, tb = sys.exc_info()
assert exc_value is e

# ensure not to trash sys.exc_info() at that point in case someone
# wants the traceback preserved in handle_http_exception. Of course
# we cannot prevent users from trashing it themselves in a custom
# trap_http_exception method so that's their fault then.
if isinstance(e, HTTPException) and not self.trap_http_exception(e):
return self.handle_http_exception(e)

blueprint_handlers = ()
handlers = self.error_handler_spec.get(request.blueprint)
if handlers is not None:
Expand All @@ -1376,6 +1369,13 @@ def handle_user_exception(self, e):
if isinstance(e, typecheck):
return handler(e)

# ensure not to trash sys.exc_info() at that point in case someone
# wants the traceback preserved in handle_http_exception. Of course
# we cannot prevent users from trashing it themselves in a custom
# trap_http_exception method so that's their fault then.
if isinstance(e, HTTPException) and not self.trap_http_exception(e):
return self.handle_http_exception(e)

reraise(exc_type, exc_value, tb)

def handle_exception(self, e):
Expand Down
20 changes: 19 additions & 1 deletion flask/testsuite/basic.py
Expand Up @@ -18,7 +18,7 @@
from threading import Thread
from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning
from flask._compat import text_type
from werkzeug.exceptions import BadRequest, NotFound
from werkzeug.exceptions import BadRequest, NotFound, HTTPException
from werkzeug.http import parse_date
from werkzeug.routing import BuildError

Expand Down Expand Up @@ -640,6 +640,24 @@ def error():
self.assert_equal(rv.status_code, 500)
self.assert_equal(b'internal server error', rv.data)

def test_http_error_handling(self):
app = flask.Flask(__name__)
@app.errorhandler(HTTPException)
def error(e):
if type(e) is NotFound:
return 'not here', 404
raise AssertionError(str(e))

@app.route('/explicit')
def explicit():
return flask.abort(404)

c = app.test_client()
for rv in (c.get('/not_existing'), c.get('/explicit')):
self.assert_equal(rv.data, b'not here')
self.assert_equal(rv.status_code, 404)


def test_before_request_and_routing_errors(self):
app = flask.Flask(__name__)
@app.before_request
Expand Down