1

I want to use middleware to handle some special exceptions through all project. But the middleware is almost never called when an exception is raised and I don't understand a reason of this behavior.

Middleware class:

import logging
log = logging.getLogger(__name__)

class ExceptionMiddleware(object):
    def process_exception(self, request, exception):
        log.error("Сaught exception: %s"% exception)

My Middleware classes:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
     # Exception middleware
    'middleware.ExceptionMiddleware',
 )

Maybe I found a reason. Instance of the specific class is created out of the all view as global value and exception is raised before some view is called. So another part of question, how I can catch those exceptions?

Adin
  • 101
  • 1
  • 4
  • Did u add the ExceptionMiddleware class in MIDDLEWARE_CLASSES – shiva Apr 28 '12 at 09:08
  • Yes, but only some exceptions are caught. – Adin Apr 28 '12 at 09:17
  • can u also add the code where you are raising the exception – shiva Apr 28 '12 at 09:20
  • Exceptions are raised from the library that I use. – Adin Apr 28 '12 at 09:35
  • make sure that your exception are raised inside your view function.since process_exception handles exceptions that are raised inside view.[see here](https://docs.djangoproject.com/en/dev/topics/http/middleware/?from=olddocs#process-exception) – shiva Apr 28 '12 at 09:54

0 Answers0