Browse Source

Add Module.app_errorhandler, like Flask.errorhandler.

pull/124/head
Simon Sapin 15 years ago committed by Armin Ronacher
parent
commit
7a4b608839
  1. 9
      flask.py
  2. 26
      tests/flask_tests.py

9
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)

26
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):

Loading…
Cancel
Save