mirror of https://github.com/django/django.git
Fixed #616 -- Added a process_exception() hook to middleware framework. Thanks, Hugo
git-svn-id: http://code.djangoproject.com/svn/django/trunk@880 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
24154b2166
commit
a2e26150b7
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
----------
|
||||
|
||||
|
|
Loading…
Reference in New Issue