Exceptions thrown by request middleware are not caught
|Reported by:||Simon Willison||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
At the moment, an exception thrown by request middleware is not caught by Django's exception handling middleware, and can result in an ugly Apache default 500 error screen. This also means that you can't throw an Http404 exception in request middleware, which is annoying as an obvious use for it is to apply additional URL filtering or blocking.
My particular use case is throwing a 404 for unknown subdomains while using wildcard DNS. At the moment I'm having to use view middleware for this instead to ensure the 404 gets caught.
Is there a reason that request middleware is executed outside the main try/catch block, or could it be moved in there without breaking anything?
Here's the code in question.