Ticket #616: process-exception.diff
File process-exception.diff, 3.7 KB (added by , 19 years ago) |
---|
-
django/utils/decorators.py
12 12 result = middleware.process_request(request) 13 13 if result is not None: 14 14 return result 15 response = view_func(request, *args, **kwargs) 15 try: 16 response = view_func(request, *args, **kwargs) 17 except Exception, e: 18 if hasattr(middleweare, 'process_exception'): 19 result = middleware.process_exception(request, e) 20 if result is not None: 21 return result 22 raise e 16 23 if hasattr(middleware, 'process_response'): 17 24 result = middleware.process_response(request, response) 18 25 if result is not None: -
django/core/handlers/base.py
2 2 3 3 class BaseHandler: 4 4 def __init__(self): 5 self._request_middleware = self._view_middleware = self._response_middleware = None5 self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None 6 6 7 7 def load_middleware(self): 8 8 """ … … 15 15 self._request_middleware = [] 16 16 self._view_middleware = [] 17 17 self._response_middleware = [] 18 self._exception_middleware = [] 18 19 for middleware_path in settings.MIDDLEWARE_CLASSES: 19 20 dot = middleware_path.rindex('.') 20 21 mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:] … … 38 39 self._view_middleware.append(mw_instance.process_view) 39 40 if hasattr(mw_instance, 'process_response'): 40 41 self._response_middleware.insert(0, mw_instance.process_response) 42 if hasattr(mw_instance, 'process_exception'): 43 self._exception_middleware.insert(0, mw_instance.process_exception) 41 44 42 45 def get_response(self, path, request): 43 46 "Returns an HttpResponse object for the given HttpRequest" … … 61 64 if response: 62 65 return response 63 66 64 response = callback(request, **param_dict) 67 try: 68 response = callback(request, **param_dict) 69 except Exception, e: 70 # if there is an exception, run it through exception middleware and 71 # if a response is given, return that. Otherwise reraise the exception. 72 for middleware_method in self._exception_middleware: 73 response = middleware_method(request, e) 74 if response: 75 return response 76 raise e 65 77 66 78 # Complain if the view returned None (a common error). 67 79 if response is None: -
docs/middleware.txt
168 168 the given ``response``, or it could create and return a brand-new 169 169 ``HttpResponse``. 170 170 171 process_exception 172 ----------------- 173 174 Interface: ``process_exception(self, request, exception)`` 175 176 ``request`` is an ``HttpRequest`` object. ``exception`` is an ``Exception`` 177 object raised by the view function. 178 179 ``process_exception()`` can return an ``HttpResponse`` object. If it does, 180 that response will be returned to the browser. Otherwise system exception 181 handling kicks in. 182 171 183 Guidelines 172 184 ---------- 173 185