diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index 00149ff791..ecec674d3e 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -2,7 +2,7 @@ from django.utils import httpwrappers class BaseHandler: def __init__(self): - self._request_middleware = self._view_middleware = self._response_middleware = None + self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None def load_middleware(self): """ @@ -15,6 +15,7 @@ class BaseHandler: self._request_middleware = [] self._view_middleware = [] self._response_middleware = [] + self._exception_middleware = [] for middleware_path in settings.MIDDLEWARE_CLASSES: dot = middleware_path.rindex('.') mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] @@ -38,6 +39,8 @@ class BaseHandler: self._view_middleware.append(mw_instance.process_view) if hasattr(mw_instance, 'process_response'): self._response_middleware.insert(0, mw_instance.process_response) + if hasattr(mw_instance, 'process_exception'): + self._exception_middleware.insert(0, mw_instance.process_exception) def get_response(self, path, request): "Returns an HttpResponse object for the given HttpRequest" @@ -61,7 +64,17 @@ class BaseHandler: if response: return response - response = callback(request, **param_dict) + try: + response = callback(request, **param_dict) + except Exception, e: + # 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. + for middleware_method in self._exception_middleware: + response = middleware_method(request, e) + if response: + return response + raise e # Complain if the view returned None (a common error). if response is None: diff --git a/django/utils/decorators.py b/django/utils/decorators.py index 1333f9da88..074532e741 100644 --- a/django/utils/decorators.py +++ b/django/utils/decorators.py @@ -16,7 +16,14 @@ def decorator_from_middleware(middleware_class): result = middleware.process_view(request, view_func, **kwargs) if result is not None: return result - response = view_func(request, *args, **kwargs) + try: + response = view_func(request, *args, **kwargs) + except Exception, e: + if hasattr(middleware, 'process_exception'): + result = middleware.process_exception(request, e) + if result is not None: + return result + raise e if hasattr(middleware, 'process_response'): result = middleware.process_response(request, response) if result is not None: diff --git a/docs/middleware.txt b/docs/middleware.txt index dfa1947bbd..33cb1a38e4 100644 --- a/docs/middleware.txt +++ b/docs/middleware.txt @@ -168,6 +168,19 @@ object returned by a Django view. the given ``response``, or it could create and return a brand-new ``HttpResponse``. +process_exception +----------------- + +Interface: ``process_exception(self, request, exception)`` + +``request`` is an ``HttpRequest`` object. ``exception`` is an ``Exception`` +object raised by the view function. + +Django calls ``process_exception()`` when a view raises an exception. +``process_exception()`` should return either ``None`` or an ``HttpResponse`` +object. If it returns an ``HttpResponse`` object, the response will be returned +to the browser. Otherwise, default exception handling kicks in. + Guidelines ----------