Invoke after_request on exceptions as well. This fixes #59
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent ee69fb5 · Browse files · 2 files·+45 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+45 added−0 removed
- tests:+32 added−0 removed
- other:+13 added−0 removed
Provenance
- Commit
33e7f2b- Tree
aae924c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ee69fb5
2 files changed+45 added−0 removed
modified:tests/flask_tests.py+32 added
14 unmodified lines15 import sys16 import flask17 import unittest18 import tempfile19+from logging import StreamHandler20 from contextlib import contextmanager21 from datetime import datetime22 from werkzeug import parse_date, parse_options_header23 from cStringIO import StringIO216 unmodified lines240 rv = app.test_client().get('/').data241 assert 'after' in evts242 assert rv == 'request|after'243 244+ def test_after_request_errors(self):245+ app = flask.Flask(__name__)246+ called = []247+ @app.after_request248+ def after_request(response):249+ called.append(True)250+ return response251+ @app.route('/')252+ def fails():253+ 1/0254+ rv = app.test_client().get('/')255+ assert rv.status_code == 500256+ assert 'Internal Server Error' in rv.data257+ assert len(called) == 1258+259+ def test_after_request_handler_error(self):260+ error_out = StringIO()261+ app = flask.Flask(__name__)262+ app.logger.addHandler(StreamHandler(error_out))263+ @app.after_request264+ def after_request(response):265+ 1/0266+ return response267+ @app.route('/')268+ def fails():269+ 1/0270+ rv = app.test_client().get('/')271+ assert rv.status_code == 500272+ assert 'Internal Server Error' in rv.data273+ assert 'after_request handler failed' in error_out.getvalue()274+275 def test_error_handling(self):276 app = flask.Flask(__name__)277 @app.errorhandler(404)278 def not_found(e):538 unmodified linesmodified:flask.py+13 added
1360 unmodified lines1361 1362 Then you still have the original application object around and1363 can continue to call methods on it.1364 1365+ .. versionchanged:: 0.41366+ The :meth:`after_request` functions are now called even if an1367+ error handler took over request processing. This ensures that1368+ even if an exception happens database have the chance to1369+ properly close the connection.1370+1371 :param environ: a WSGI environment1372 :param start_response: a callable accepting a status code,1373 a list of headers and an optional1374 exception context to start the response6 unmodified lines1381 response = self.make_response(rv)1382 response = self.process_response(response)1383 except Exception, e:1384 response = self.make_response(self.handle_exception(e))1385+ try:1386+ response = self.process_response(response)1387+ except Exception, e:1388+ self.logger.exception('after_request handler failed '1389+ 'to postprocess error response. '1390+ 'Depending on uncertain state?')1391+1392 return response(environ, start_response)1393 1394 def request_context(self, environ):1395 """Creates a request context from the given environment and binds55 unmodified lines