Middleware process_exception not called when another middleware raises an error
|Reported by:||Anssi Kääriäinen||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
It seems that process_exception isn't called at all if anther middleware below it raises an exception in process_request. This has some interesting side-effects. For example, if you have this setting:
MIDDLEWARE_CLASSES = [ 'TransactionMiddleware', 'SavesToDB', 'RaisesException' ]
The end result is that first, transaction middleware will open a transaction, second SavesToDB will save to DB, finally RaisesException raises an exception. Now, process_exception isn't called, we go directly to process_response stage. assume that RaisesException and SavesToDB do nothing. So, finally transaction middleware commits the transaction. Which seems like exactly the wrong thing to do. My interpretation is that if an exception happens below transaction middleware, then the transaction will be rolled back, no matter if the source is the view or another middleware.
IMO we should do a process_exception() dance in case another middleware raises an exception. Probably we should go just through those middleware classes before the exception.
Another option is do immediate 500 response and not proceed to do anything else if process_request raises an exception. Middleware's process_request should not throw an error, so this would be "critical error, bail out immediately" response to erroneous middleware.