Opened 5 years ago

Closed 4 years ago

#16367 closed New feature (wontfix)

Exceptions thrown by middleware.process_response don't get to middleware.process_exception

Reported by: bob84123 Owned by: Aymeric Augustin
Component: Core (Other) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I have a custom exception handler, implemented as a middleware class with a process_exception method, this handler does not receive an exception that is thrown during another middleware's process_response method. This mainly happens with django.middleware.transaction.TransactionMiddleware; if a view executes a query that violates a PostgreSQL constraint, an exception isn't thrown until the transaction is committed, which is inside the middleware, and so my exception logger can't catch that.

I have a patch that fixes the issue and doesn't break anything else (according to the test suite). I can't see any downsides of applying this, but I may be missing something, so let me know.

Attachments (1)

response_middleware_exception_fix.diff (1.2 KB) - added by bob84123 5 years ago.

Download all attachments as: .zip

Change History (5)

Changed 5 years ago by bob84123

comment:1 Changed 5 years ago by Aymeric Augustin

Component: HTTP handlingCore (Other)
Needs documentation: set
Needs tests: set
Triage Stage: UnreviewedDesign decision needed
Type: BugNew feature

Exception middleware currently works as advertised.

https://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception:

Django calls process_exception() when a view raises an exception.

You're proposing to change its behavior, so that it will also be called if an exception happens in a middleware. This requires a decision by a core developer.

Version 0, edited 5 years ago by Aymeric Augustin (next)

comment:2 Changed 5 years ago by anonymous

It would to be great to have process_exception called for exceptions that happen in a middleware. Hope there is progress on this bug soon.

comment:3 Changed 4 years ago by Aymeric Augustin

Owner: changed from nobody to Aymeric Augustin
Status: newassigned

comment:4 Changed 4 years ago by Aymeric Augustin

Resolution: wontfix
Status: assignedclosed

Overall, the design of Django's middleware assumes that middleware is reliable and will not throw exceptions under normal circumstances.

Besides, the current behavior is documented.

Since TransactionMiddleware is now deprecated (it's basically impossible to implement this feature reliable as a middleware), the original use case has disappeared, and I'm going to close this ticket.

Note: See TracTickets for help on using tickets.
Back to Top