mirror of https://github.com/pallets/flask.git
Register errorhandlers for Exceptions
Allow a default errorhandler by registering an errorhandler for HTTPException tests included
This commit is contained in:
parent
c8e56d5807
commit
668061a5fc
11
flask/app.py
11
flask/app.py
|
|
@ -1484,7 +1484,16 @@ class Flask(_PackageBoundObject):
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
# fall back to app handlers
|
# fall back to app handlers
|
||||||
return find_handler(self.error_handler_spec[None].get(code))
|
handler = find_handler(self.error_handler_spec[None].get(code))
|
||||||
|
if handler is not None:
|
||||||
|
return handler
|
||||||
|
|
||||||
|
try:
|
||||||
|
handler = find_handler(self.error_handler_spec[None][None])
|
||||||
|
except KeyError:
|
||||||
|
handler = None
|
||||||
|
|
||||||
|
return handler
|
||||||
|
|
||||||
def handle_http_exception(self, e):
|
def handle_http_exception(self, e):
|
||||||
"""Handles an HTTP exception. By default this will invoke the
|
"""Handles an HTTP exception. By default this will invoke the
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from werkzeug.exceptions import Forbidden, InternalServerError
|
from werkzeug.exceptions import Forbidden, InternalServerError, HTTPException, NotFound
|
||||||
import flask
|
import flask
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -32,6 +32,29 @@ def test_error_handler_no_match():
|
||||||
assert c.get('/keyerror').data == b'KeyError'
|
assert c.get('/keyerror').data == b'KeyError'
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_error_handler():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
@app.errorhandler(HTTPException)
|
||||||
|
def catchall_errorhandler(e):
|
||||||
|
assert isinstance(e, HTTPException)
|
||||||
|
assert isinstance(e, NotFound)
|
||||||
|
return 'default'
|
||||||
|
|
||||||
|
@app.errorhandler(Forbidden)
|
||||||
|
def catchall_errorhandler(e):
|
||||||
|
assert isinstance(e, Forbidden)
|
||||||
|
return 'forbidden'
|
||||||
|
|
||||||
|
@app.route('/forbidden')
|
||||||
|
def forbidden():
|
||||||
|
raise Forbidden()
|
||||||
|
|
||||||
|
c = app.test_client()
|
||||||
|
assert c.get('/undefined').data == b'default'
|
||||||
|
assert c.get('/forbidden').data == b'forbidden'
|
||||||
|
|
||||||
|
|
||||||
def test_error_handler_subclass():
|
def test_error_handler_subclass():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue