@@ -1555,7 +1555,7 @@ def handle_exception(self, e):
1555
1555
self .log_exception ((exc_type , exc_value , tb ))
1556
1556
if handler is None :
1557
1557
return InternalServerError ()
1558
- return handler (e )
1558
+ return self . finalize_request ( handler (e ), from_error_handler = True )
1559
1559
1560
1560
def log_exception (self , exc_info ):
1561
1561
"""Logs an exception. This is called by :meth:`handle_exception`
@@ -1623,9 +1623,30 @@ def full_dispatch_request(self):
1623
1623
rv = self .dispatch_request ()
1624
1624
except Exception as e :
1625
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 finalizes
1630
+ the request by converting it into a repsonse and invoking the
1631
+ postprocessing functions. This is invoked for both normal
1632
+ request dispatching as well as error handlers.
1633
+
1634
+ Because this means that it might be called as a result of a
1635
+ failure a special safe mode is available which can be enabled
1636
+ with the `from_error_handler` flag. If enabled failures in
1637
+ response processing will be logged and otherwise ignored.
1638
+
1639
+ :internal:
1640
+ """
1626
1641
response = self .make_response (rv )
1627
- response = self .process_response (response )
1628
- request_finished .send (self , response = response )
1642
+ try :
1643
+ response = self .process_response (response )
1644
+ request_finished .send (self , response = response )
1645
+ except Exception :
1646
+ if not from_error_handler :
1647
+ raise
1648
+ self .logger .exception ('Request finalizing failed with an '
1649
+ 'error while handling an error' )
1629
1650
return response
1630
1651
1631
1652
def try_trigger_before_first_request_functions (self ):
@@ -1972,7 +1993,7 @@ def wsgi_app(self, environ, start_response):
1972
1993
response = self .full_dispatch_request ()
1973
1994
except Exception as e :
1974
1995
error = e
1975
- response = self .make_response ( self . handle_exception (e ) )
1996
+ response = self .handle_exception (e )
1976
1997
return response (environ , start_response )
1977
1998
finally :
1978
1999
if self .should_ignore_error (error ):
0 commit comments