Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #616 -- Added a process_exception() hook to middleware framewor…

…k. Thanks, Hugo

git-svn-id: http://code.djangoproject.com/svn/django/trunk@880 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a2e26150b77cd2cdad4cc9de120a87a6370c6dd5 1 parent 24154b2
@adrianholovaty adrianholovaty authored
View
17 django/core/handlers/base.py
@@ -2,7 +2,7 @@
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 @@ def load_middleware(self):
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 @@ def load_middleware(self):
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 @@ def get_response(self, path, request):
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:
View
9 django/utils/decorators.py
@@ -16,7 +16,14 @@ def _wrapped_view(request, *args, **kwargs):
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:
View
13 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
----------
Please sign in to comment.
Something went wrong with that request. Please try again.