Skip to content

Commit

Permalink
Fixed last commit and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 21, 2013
1 parent f1537a9 commit 61d3bbf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
12 changes: 6 additions & 6 deletions flask/app.py
Expand Up @@ -1283,6 +1283,10 @@ def handle_http_exception(self, e):
.. versionadded:: 0.3 .. versionadded:: 0.3
""" """
handlers = self.error_handler_spec.get(request.blueprint) 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: if handlers and e.code in handlers:
handler = handlers[e.code] handler = handlers[e.code]
else: else:
Expand Down Expand Up @@ -1327,12 +1331,8 @@ def handle_user_exception(self, e):
# ensure not to trash sys.exc_info() at that point in case someone # ensure not to trash sys.exc_info() at that point in case someone
# wants the traceback preserved in handle_http_exception. Of course # wants the traceback preserved in handle_http_exception. Of course
# we cannot prevent users from trashing it themselves in a custom # we cannot prevent users from trashing it themselves in a custom
# trap_http_exception method so that's their fault then. Proxy # trap_http_exception method so that's their fault then.
# exceptions generally must always be trapped (filtered out by if isinstance(e, HTTPException) and not self.trap_http_exception(e):
# e.code == None)
if isinstance(e, HTTPException) \
and e.code is not None \
and not self.trap_http_exception(e):
return self.handle_http_exception(e) return self.handle_http_exception(e)


blueprint_handlers = () blueprint_handlers = ()
Expand Down
25 changes: 25 additions & 0 deletions flask/testsuite/regression.py
Expand Up @@ -86,7 +86,32 @@ def test_safe_join_toplevel_pardir(self):
safe_join('/foo', '..') 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(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MemoryTestCase)) suite.addTest(unittest.makeSuite(MemoryTestCase))
suite.addTest(unittest.makeSuite(ExceptionTestCase))
return suite return suite

0 comments on commit 61d3bbf

Please sign in to comment.