diff --git a/flask.py b/flask.py index bf4451c7..66ce172f 100644 --- a/flask.py +++ b/flask.py @@ -612,6 +612,15 @@ class Module(_PackageBoundObject): .setdefault(None, []).append(f)) return f + def app_errorhandler(self, code): + """Like :meth:`Flask.errorhandler` but for a module. This + handler is used for all requests, even if outside of the module. + """ + def decorator(f): + self._record(lambda s: s.app.errorhandler(code)(f)) + return f + return decorator + def _record(self, func): self._register_events.append(func) diff --git a/tests/flask_tests.py b/tests/flask_tests.py index ff95364f..9d1dba6e 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -260,7 +260,7 @@ class BasicFunctionalityTestCase(unittest.TestCase): assert rv.data == 'not found' rv = c.get('/error') assert rv.status_code == 500 - assert 'internal server error' in rv.data + assert 'internal server error' == rv.data def test_response_creation(self): app = flask.Flask(__name__) @@ -536,6 +536,30 @@ class ModuleTestCase(unittest.TestCase): app.register_module(admin, url_prefix='/admin') assert app.test_client().get('/admin/').data == '42' + def test_error_handling(self): + app = flask.Flask(__name__) + admin = flask.Module(__name__, 'admin') + @admin.app_errorhandler(404) + def not_found(e): + return 'not found', 404 + @admin.app_errorhandler(500) + def internal_server_error(e): + return 'internal server error', 500 + @admin.route('/') + def index(): + flask.abort(404) + @admin.route('/error') + def error(): + 1 // 0 + app.register_module(admin) + c = app.test_client() + rv = c.get('/') + assert rv.status_code == 404 + assert rv.data == 'not found' + rv = c.get('/error') + assert rv.status_code == 500 + assert 'internal server error' == rv.data + class SendfileTestCase(unittest.TestCase):