Skip to content

Commit

Permalink
Add Module.app_errorhandler, like Flask.errorhandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin authored and mitsuhiko committed May 31, 2010
1 parent a224fec commit 7a4b608
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions flask.py
Expand Up @@ -612,6 +612,15 @@ def app_context_processor(self, f):
.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)

Expand Down
26 changes: 25 additions & 1 deletion tests/flask_tests.py
Expand Up @@ -260,7 +260,7 @@ def error():
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__)
Expand Down Expand Up @@ -536,6 +536,30 @@ def index():
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):

Expand Down

0 comments on commit 7a4b608

Please sign in to comment.