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/app.py
View
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: http://fabfile.org/
View
23 docs/extensiondev.rst
@@ -290,6 +290,29 @@ and bind their app to the extension in another file::
manager.init_app(app)
+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 = _request_ctx_stack.top
+ 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
-----------------
View
13 flask/app.py
@@ -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]' % (
request.path,
request.method

0 comments on commit 086ecdb

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