Code

Opened 8 years ago

Closed 6 years ago

#2481 closed defect (duplicate)

Exceptions thrown by request middleware are not caught

Reported by: Simon Willison Owned by: nobody
Component: Core (Other) Version:
Severity: minor Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

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.

Attachments (0)

Change History (3)

comment:1 Changed 7 years ago by Simon G. <dev@…>

  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by kac.gigant@…

I would like to add some suggestions of mine. I think that some thing like DjangoExceptionHandler should be added it could handle all known exceptions thrown in this huge try ... except used in get_response http://code.djangoproject.com/browser/django/trunk/django/core/handlers/base.py?rev=3411#L57. It would be great if it could be replaced from settings by any other user class.
I think that call to mail_admins function is very annoying because it cannot be turned off. It could be one of features off DjangoExceptionHandler.
Another thing is that Http404 and PermissionDenied exceptions are not flexible. There is no way to change behavior of django when they are thrown.

comment:3 Changed 6 years ago by gwilson

  • Resolution set to duplicate
  • Status changed from new to closed

marking dup of #6094, as there is a patch there.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.