From 61d3bbf1d237cd8f03e37af88ad4a6b59f27d4e1 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 21 Jan 2013 17:55:07 +0000 Subject: [PATCH] Fixed last commit and added test --- flask/app.py | 12 ++++++------ flask/testsuite/regression.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/flask/app.py b/flask/app.py index 5d87b1e8..f6d40be2 100644 --- a/flask/app.py +++ b/flask/app.py @@ -1283,6 +1283,10 @@ class Flask(_PackageBoundObject): .. versionadded:: 0.3 """ handlers = self.error_handler_spec.get(request.blueprint) + # Proxy exceptions don't have error codes. We want to always return + # those unchanged as errors + if e.code is None: + return e if handlers and e.code in handlers: handler = handlers[e.code] else: @@ -1327,12 +1331,8 @@ class Flask(_PackageBoundObject): # ensure not to trash sys.exc_info() at that point in case someone # wants the traceback preserved in handle_http_exception. Of course # we cannot prevent users from trashing it themselves in a custom - # trap_http_exception method so that's their fault then. Proxy - # exceptions generally must always be trapped (filtered out by - # e.code == None) - if isinstance(e, HTTPException) \ - and e.code is not None \ - and not self.trap_http_exception(e): + # trap_http_exception method so that's their fault then. + if isinstance(e, HTTPException) and not self.trap_http_exception(e): return self.handle_http_exception(e) blueprint_handlers = () diff --git a/flask/testsuite/regression.py b/flask/testsuite/regression.py index 87a6289b..34fdefc0 100644 --- a/flask/testsuite/regression.py +++ b/flask/testsuite/regression.py @@ -86,7 +86,32 @@ class MemoryTestCase(FlaskTestCase): safe_join('/foo', '..') +class ExceptionTestCase(FlaskTestCase): + + def test_aborting(self): + class Foo(Exception): + whatever = 42 + app = flask.Flask(__name__) + app.testing = True + @app.errorhandler(Foo) + def handle_foo(e): + return str(e.whatever) + @app.route('/') + def index(): + raise flask.abort(flask.redirect(flask.url_for('test'))) + @app.route('/test') + def test(): + raise Foo() + + with app.test_client() as c: + rv = c.get('/') + self.assertEqual(rv.headers['Location'], 'http://localhost/test') + rv = c.get('/test') + self.assertEqual(rv.data, '42') + + def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(MemoryTestCase)) + suite.addTest(unittest.makeSuite(ExceptionTestCase)) return suite