Opened 5 years ago

Closed 3 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: aaugustin
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


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 aaugustin

  • Component changed from HTTP handling to Core (Other)
  • Needs documentation set
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed
  • Type changed from Bug to New feature

Exception middleware currently works as advertised.

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.

Last edited 5 years ago by aaugustin (previous) (diff)

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 3 years ago by aaugustin

  • Owner changed from nobody to aaugustin
  • Status changed from new to assigned

comment:4 Changed 3 years ago by aaugustin

  • Resolution set to wontfix
  • Status changed from assigned to closed

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