Better reraising of exceptions
This commit is contained in:
parent
b51ecd7f21
commit
086ecdb918
3 changed files with 37 additions and 2 deletions
|
|
@ -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/
|
||||
|
|
|
|||
|
|
@ -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
|
||||
-----------------
|
||||
|
||||
|
|
|
|||
13
flask/app.py
13
flask/app.py
|
|
@ -793,10 +793,21 @@ class Flask(_PackageBoundObject):
|
|||
|
||||
.. 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue