Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert "Revert "Merge pull request #859 from wvh/register_error_handl…

…er""

This reverts commit 2aa26ff.
  • Loading branch information...
commit d4fec1454488687b574c545761c2869d3bb97ca6 1 parent 2aa26ff
@kennethreitz kennethreitz authored
Showing with 44 additions and 0 deletions.
  1. +11 −0 flask/blueprints.py
  2. +33 −0 flask/testsuite/blueprints.py
View
11 flask/blueprints.py
@@ -399,3 +399,14 @@ def decorator(f):
self.name, code_or_exception, f))
return f
return decorator
+
+ def register_error_handler(self, code_or_exception, f):
+ """Non-decorator version of the :meth:`errorhandler` error attach
+ function, akin to the :meth:`~flask.Flask.register_error_handler`
+ application-wide function of the :class:`~flask.Flask` object but
+ for error handlers limited to this blueprint.
+
+ .. versionadded:: 0.11
+ """
+ self.record_once(lambda s: s.app._register_error_handler(
+ self.name, code_or_exception, f))
View
33 flask/testsuite/blueprints.py
@@ -296,6 +296,39 @@ def app_forbidden(e):
self.assert_equal(c.get('/backend-no').data, b'backend says no')
self.assert_equal(c.get('/what-is-a-sideend').data, b'application itself says no')
+ def test_blueprint_specific_user_error_handling(self):
+ class MyDecoratorException(Exception):
+ pass
+ class MyFunctionException(Exception):
+ pass
+
+ blue = flask.Blueprint('blue', __name__)
+
+ @blue.errorhandler(MyDecoratorException)
+ def my_decorator_exception_handler(e):
+ self.assert_true(isinstance(e, MyDecoratorException))
+ return 'boom'
+
+ def my_function_exception_handler(e):
+ self.assert_true(isinstance(e, MyFunctionException))
+ return 'bam'
+ blue.register_error_handler(MyFunctionException, my_function_exception_handler)
+
+ @blue.route('/decorator')
+ def blue_deco_test():
+ raise MyDecoratorException()
+ @blue.route('/function')
+ def blue_func_test():
+ raise MyFunctionException()
+
+ app = flask.Flask(__name__)
+ app.register_blueprint(blue)
+
+ c = app.test_client()
+
+ self.assert_equal(c.get('/decorator').data, b'boom')
+ self.assert_equal(c.get('/function').data, b'bam')
+
def test_blueprint_url_definitions(self):
bp = flask.Blueprint('test', __name__)
Please sign in to comment.
Something went wrong with that request. Please try again.