Ticket #616: process-exception.diff

File process-exception.diff, 3.7 KB (added by hugo, 19 years ago)

process_exception extension to middleware (now with support for decorator_from_middleware)

  • django/utils/decorators.py

     
    1212                result = middleware.process_request(request)
    1313                if result is not None:
    1414                    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
    1623            if hasattr(middleware, 'process_response'):
    1724                result = middleware.process_response(request, response)
    1825                if result is not None:
  • django/core/handlers/base.py

     
    22
    33class BaseHandler:
    44    def __init__(self):
    5         self._request_middleware = self._view_middleware = self._response_middleware = None
     5        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None
    66
    77    def load_middleware(self):
    88        """
     
    1515        self._request_middleware = []
    1616        self._view_middleware = []
    1717        self._response_middleware = []
     18        self._exception_middleware = []
    1819        for middleware_path in settings.MIDDLEWARE_CLASSES:
    1920            dot = middleware_path.rindex('.')
    2021            mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
     
    3839                self._view_middleware.append(mw_instance.process_view)
    3940            if hasattr(mw_instance, 'process_response'):
    4041                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)
    4144
    4245    def get_response(self, path, request):
    4346        "Returns an HttpResponse object for the given HttpRequest"
     
    6164                if response:
    6265                    return response
    6366
    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
    6577
    6678            # Complain if the view returned None (a common error).
    6779            if response is None:
  • docs/middleware.txt

     
    168168the given ``response``, or it could create and return a brand-new
    169169``HttpResponse``.
    170170
     171process_exception
     172-----------------
     173
     174Interface: ``process_exception(self, request, exception)``
     175
     176``request`` is an ``HttpRequest`` object. ``exception`` is an ``Exception``
     177object raised by the view function.
     178
     179``process_exception()`` can return an ``HttpResponse`` object. If it does,
     180that response will be returned to the browser. Otherwise system exception
     181handling kicks in.
     182
    171183Guidelines
    172184----------
    173185
Back to Top