Skip to content
Browse files

Better reraising of exceptions

  • Loading branch information...
1 parent b51ecd7 commit 086ecdb91829d43a9b948c6d97524d8de18e2fa4 @mitsuhiko mitsuhiko committed
Showing with 37 additions and 2 deletions.
  1. +2 −1 docs/config.rst
  2. +23 −0 docs/extensiondev.rst
  3. +12 −1 flask/
3 docs/config.rst
@@ -240,6 +240,7 @@ your configuration files. However here a list of good recommendations:
and exports the development configuration for you.
- Use a tool like `fabric`_ in production to push code and
configurations separately to the production server(s). For some
- details about how to do that, head over to the :ref:`deploy` pattern.
+ details about how to do that, head over to the
+ :ref:`fabric-deployment` pattern.
.. _fabric:
23 docs/extensiondev.rst
@@ -290,6 +290,29 @@ and bind their app to the extension in another file::
+End-Of-Request Behavior
+Due to the change in Flask 0.7 regarding functions that are run at the end
+of the request your extension will have to be extra careful there if it
+wants to continue to support older versions of Flask. The following
+pattern is a good way to support both::
+ def close_connection(response):
+ ctx =
+ ctx.sqlite3_db.close()
+ return response
+ if hasattr(app, 'teardown_request'):
+ app.teardown_request(close_connection)
+ else:
+ app.after_request(close_connection)
+Strictly speaking the above code is wrong, because teardown functions are
+passed the exception and typically don't return anything. However because
+the return value is discarded this will just work assuming that the code
+in between does not touch the passed parameter.
Learn from Others
13 flask/
@@ -793,10 +793,21 @@ def handle_exception(self, e):
.. versionadded: 0.3
+ exc_type, exc_value, tb = sys.exc_info()
got_request_exception.send(self, exception=e)
handler = self.error_handlers.get(500)
if self.propagate_exceptions:
- raise
+ # if we want to repropagate the exception, we can attempt to
+ # raise it with the whole traceback in case we can do that
+ # (the function was actually called from the except part)
+ # otherwise, we just raise the error again
+ if exc_value is e:
+ raise exc_type, exc_value, tb
+ else:
+ raise e
self.logger.exception('Exception on %s [%s]' % (

0 comments on commit 086ecdb

Please sign in to comment.
Something went wrong with that request. Please try again.