Ticket #16674: wsgi-expose-exc-info.patch

File wsgi-expose-exc-info.patch, 2.0 KB (added by James Henstridge, 8 years ago)
  • django/core/handlers/wsgi.py

     
    11import sys
    2 from threading import Lock
     2from threading import Lock, local
    33try:
    44    from cStringIO import StringIO
    55except ImportError:
     
    194194    initLock = Lock()
    195195    request_class = WSGIRequest
    196196
     197    def __init__(self):
     198        super(WSGIHandler, self).__init__()
     199        self._tls = local()
     200
    197201    def __call__(self, environ, start_response):
    198202        from django.conf import settings
    199203
     
    215219
    216220        set_script_prefix(base.get_script_name(environ))
    217221        signals.request_started.send(sender=self.__class__)
     222        self._tls.exc_info = None
    218223        try:
    219224            try:
    220225                request = self.request_class(environ)
    221226            except UnicodeDecodeError:
     227                self._tls.exc_info = sys.exc_info()
    222228                logger.warning('Bad Request (UnicodeDecodeError)',
    223                     exc_info=sys.exc_info(),
     229                    exc_info=self._tls.exc_info,
    224230                    extra={
    225231                        'status_code': 400,
    226232                    }
     
    239245        response_headers = [(str(k), str(v)) for k, v in response.items()]
    240246        for c in response.cookies.values():
    241247            response_headers.append(('Set-Cookie', str(c.output(header=''))))
    242         start_response(status, response_headers)
     248        try:
     249            start_response(status, response_headers, self._tls.exc_info)
     250        finally:
     251            self._tls.exc_info = None
    243252        return response
    244253
     254    def handle_uncaught_exception(self, request, resolver, exc_info):
     255        # Capture the exception context so we can pass it back to
     256        # __call__()
     257        self._tls.exc_info = exc_info
     258        try:
     259            return super(WSGIHandler, self).handle_uncaught_exception(
     260                request, resolver, exc_info)
     261        finally:
     262            exc_info = None
Back to Top