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?