5

I've created my own middleware class in Django that was working just fine until recently. The strange thing is that process_request still gets called just fine, but even when the response is 500 - internal server error, process_exception is not called at all. Why?

It makes no difference whether I declare my middleware class as the first or the last entry in the list of installed middleware in the settings file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Trindaz
  • 17,029
  • 21
  • 82
  • 111
  • Can it be, that process_exception itself raises an exception? – Boldewyn Jun 24 '11 at 10:57
  • 1
    Put `import pdb;pdb.set_trace()` at the beginning of your `process_exception` method, then fire up runserver and cause a 500 error. If runserver drops into a pdb session, your method is definitely getting called. Then, step through to see what happens. – Chris Pratt Jun 24 '11 at 15:47
  • I am getting the same issue. I want to add details to the exception email. I found a solution from http://stackoverflow.com/questions/6678895/django-error-reporting-how-to-know-which-user-triggered-the-error. I implemented and the process_exception() doesn't seem to run. I added a process_request() method and appended the request.META['MYVAR'] = "funky" and that gets included in the emails, but I can't seem to get process_exception() to fire. I also have tried different locations (first and last) in the MIDDLEWARE_CLASSES setting. – Furbeenator Oct 05 '11 at 17:33
  • I tried excluding middleware classes until I narrowed it down to the AuthenticationMiddle class. When that one is excluded, my process_exception() runs, but if it is enabled, mine does not run, the standard one does. – Furbeenator Oct 05 '11 at 18:58
  • @Justin Poliey This question is almost a year old and I don't have quick access to the code that would let me verify solutions. Is it my responsibility to select an answer for the bounty to be awarded? – Trindaz May 08 '12 at 16:00

2 Answers2

10

The process_exception only gets invoked when the view raises an Exception. As it says in the comment:

If the view raised an exception, run it through exception middleware, and if the exception middleware returns a response, use that. Otherwise, reraise the exception.

Exceptions raised by misconfiguration, importing error, process_request and process_view cannot be caught and feed to process_exception handlers.

To test whether your process_exception works, raise an Exception in the view after you ensure it works well.

There is not direct relationship between process_request and process_exception. They are handlers for different purposes and get invoked at different stages. Any Exception been raised after the process_request that executed successfully and before the view, will not be caught and processed by process_exception as said.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
okm
  • 23,575
  • 5
  • 83
  • 90
2

As per the documentation for process_exception,

process_exception(self, request, exception) takes an HttpRequest object, an Exception object raised by the view function as arguments and returns an HttpResponseobject as response. So, it will not be called when the code raises a 500 error (an HttpResponse object). The process_exception will be called only for uncaught exceptions in the views.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivek S
  • 5,384
  • 8
  • 51
  • 72