Skip to content
Myr

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

2 files+45 added−0 removed

modified:tests/flask_tests.py+32 added
14 unmodified lines
15import sys16import flask17import unittest18import tempfile19from logging import StreamHandler20from contextlib import contextmanager21from datetime import datetime22from werkzeug import parse_date, parse_options_header23from cStringIO import StringIO
216 unmodified lines
240 rv = app.test_client().get('/').data241 assert 'after' in evts242 assert rv == 'request|after'243244 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) == 1258259 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()274275 def test_error_handling(self):276 app = flask.Flask(__name__)277 @app.errorhandler(404)278 def not_found(e):
538 unmodified lines
modified:flask.py+13 added
1360 unmodified lines
13611362 Then you still have the original application object around and1363 can continue to call methods on it.13641365 .. 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.13701371 :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 response
6 unmodified lines
1381 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?')13911392 return response(environ, start_response)13931394 def request_context(self, environ):1395 """Creates a request context from the given environment and binds
55 unmodified lines