Merge pull request #2013 from pallets/bugfix/error-handler-error-handler
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Corrected after response for error handlers
authored by Armin Ronacher · committed by GitHub · parents c54c538, 9cd32ca · Browse files · 3 files·+50 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+50 added−4 removed
- tests:+23 added−0 removed
- other:+27 added−4 removed
Provenance
- Commit
9e41eca- Tree
7beb71d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
GitHub <noreply@github.com>- Parent 1
c54c538- Parent 2
9cd32ca
3 files changed+50 added−4 removed
modified:flask/app.py+25 added-4 removed
1553 unmodified lines1554 1555 self.log_exception((exc_type, exc_value, tb))1556 if handler is None:1557 return InternalServerError()1558- return handler(e)1558+ return self.finalize_request(handler(e), from_error_handler=True)1559 1560 def log_exception(self, exc_info):1561 """Logs an exception. This is called by :meth:`handle_exception`1562 if debugging is disabled and right before the handler is called.59 unmodified lines1622 if rv is None:1623 rv = self.dispatch_request()1624 except Exception as e:1625 rv = self.handle_user_exception(e)1626+ return self.finalize_request(rv)1627+1628+ def finalize_request(self, rv, from_error_handler=False):1629+ """Given the return value from a view function this finalizes1630+ the request by converting it into a repsonse and invoking the1631+ postprocessing functions. This is invoked for both normal1632+ request dispatching as well as error handlers.1633+1634+ Because this means that it might be called as a result of a1635+ failure a special safe mode is available which can be enabled1636+ with the `from_error_handler` flag. If enabled failures in1637+ response processing will be logged and otherwise ignored.1638+1639+ :internal:1640+ """1641 response = self.make_response(rv)1642+ try:1627- response = self.process_response(response)1628- request_finished.send(self, response=response)1643+ response = self.process_response(response)1644+ request_finished.send(self, response=response)1645+ except Exception:1646+ if not from_error_handler:1647+ raise1648+ self.logger.exception('Request finalizing failed with an '1649+ 'error while handling an error')1650 return response1651 1652 def try_trigger_before_first_request_functions(self):1653 """Called before each request and will ensure that it triggers338 unmodified lines1992 try:1993 response = self.full_dispatch_request()1994 except Exception as e:1995 error = e1975- response = self.make_response(self.handle_exception(e))1996+ response = self.handle_exception(e)1997 return response(environ, start_response)1998 finally:1999 if self.should_ignore_error(error):2000 error = None11 unmodified linesmodified:tests/test_basic.py+23 added
766 unmodified lines767 assert rv.status_code == 403768 assert b'forbidden' == rv.data769 770 771+def test_error_handling_processing():772+ app = flask.Flask(__name__)773+ app.config['LOGGER_HANDLER_POLICY'] = 'never'774+775+ @app.errorhandler(500)776+ def internal_server_error(e):777+ return 'internal server error', 500778+779+ @app.route('/')780+ def broken_func():781+ 1 // 0782+783+ @app.after_request784+ def after_request(resp):785+ resp.mimetype = 'text/x-special'786+ return resp787+788+ with app.test_client() as c:789+ resp = c.get('/')790+ assert resp.mimetype == 'text/x-special'791+ assert resp.data == b'internal server error'792+793+794 def test_before_request_and_routing_errors():795 app = flask.Flask(__name__)796 797 @app.before_request886 unmodified linesmodified:CHANGES+2 added
13 unmodified lines14 - Make ``flask.safe_join`` able to join multiple paths like ``os.path.join``15 (pull request ``#1730``).16 - Revert a behavior change that made the dev server crash instead of returning17 a Internal Server Error (pull request ``#2006``).18+- Correctly invoke response handlers for both regular request dispatching as19+ well as error handlers.20 21 Version 0.11.222 --------------23 572 unmodified lines