Ticket #12098: requestrepr.2.diff

File requestrepr.2.diff, 5.5 KB (added by Cody Soyland <codysoyland@…>, 14 years ago)

This patch does handles parse errors in get_full_path and adds request metadata to error emails.

  • django/core/handlers/base.py

    diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py
    index f144ce4..0ad0482 100644
    a b  
    11import sys
     2from pprint import pformat
    23
    34from django import http
    45from django.core import signals
    class BaseHandler(object):  
    170171            request_repr = repr(request)
    171172        except:
    172173            request_repr = "Request repr() unavailable"
    173         message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
     174        request_metadata = {}
     175        for (key, metadata_getter) in (
     176            ('GET', lambda request: pformat(request.GET)),
     177            ('POST', lambda request: not request._post_parse_error and pformat(request.POST) or '(could not parse)'),
     178            ('COOKIES', lambda request: pformat(request.COOKIES)),
     179            ('META', lambda request: pformat(request.META))):
     180            try:
     181                request_metadata[key] = metadata_getter(request)
     182            except Exception, e:
     183                request_metadata[key] = '(could not parse)'
     184        message = "%s\n\nRequest: %s\n\nGET: %s\n\nPOST: %s\n\nCOOKIES: %s\n\nMETA: %s\n\n" % (
     185            self._get_traceback(exc_info),
     186            request_repr,
     187            request_metadata['GET'],
     188            request_metadata['POST'],
     189            request_metadata['COOKIES'],
     190            request_metadata['META'],
     191        )
    174192        mail_admins(subject, message, fail_silently=True)
    175193        # Return an HttpResponse that displays a friendly error message.
    176194        callback, param_dict = resolver.resolve500()
  • django/core/handlers/modpython.py

    diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py
    index b1e3e17..c66fda7 100644
    a b  
    11import os
    2 from pprint import pformat
    32
    43from django import http
    54from django.core import signals
    class ModPythonRequest(http.HttpRequest):  
    3736            self.path_info = u'/'
    3837        self._post_parse_error = False
    3938
    40     def __repr__(self):
    41         # Since this is called as part of error handling, we need to be very
    42         # robust against potentially malformed input.
    43         try:
    44             get = pformat(self.GET)
    45         except:
    46             get = '<could not parse>'
    47         if self._post_parse_error:
    48             post = '<could not parse>'
    49         else:
    50             try:
    51                 post = pformat(self.POST)
    52             except:
    53                 post = '<could not parse>'
    54         try:
    55             cookies = pformat(self.COOKIES)
    56         except:
    57             cookies = '<could not parse>'
    58         try:
    59             meta = pformat(self.META)
    60         except:
    61             meta = '<could not parse>'
    62         return smart_str(u'<ModPythonRequest\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
    63                          (self.path, unicode(get), unicode(post),
    64                           unicode(cookies), unicode(meta)))
    65 
    6639    def get_full_path(self):
    6740        # RFC 3986 requires self._req.args to be in the ASCII range, but this
    6841        # doesn't always happen, so rather than crash, we defensively encode it.
  • django/core/handlers/wsgi.py

    diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
    index 927b098..646e313 100644
    a b  
    11from threading import Lock
    2 from pprint import pformat
    32try:
    43    from cStringIO import StringIO
    54except ImportError:
    class WSGIRequest(http.HttpRequest):  
    9493        self.method = environ['REQUEST_METHOD'].upper()
    9594        self._post_parse_error = False
    9695
    97     def __repr__(self):
    98         # Since this is called as part of error handling, we need to be very
    99         # robust against potentially malformed input.
    100         try:
    101             get = pformat(self.GET)
    102         except:
    103             get = '<could not parse>'
    104         if self._post_parse_error:
    105             post = '<could not parse>'
    106         else:
    107             try:
    108                 post = pformat(self.POST)
    109             except:
    110                 post = '<could not parse>'
    111         try:
    112             cookies = pformat(self.COOKIES)
    113         except:
    114             cookies = '<could not parse>'
    115         try:
    116             meta = pformat(self.META)
    117         except:
    118             meta = '<could not parse>'
    119         return '<WSGIRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
    120             (get, post, cookies, meta)
    121 
    12296    def get_full_path(self):
    12397        # RFC 3986 requires query string arguments to be in the ASCII range.
    12498        # Rather than crash if this doesn't happen, we encode defensively.
  • django/http/__init__.py

    diff --git a/django/http/__init__.py b/django/http/__init__.py
    index 446659b..ce0c86b 100644
    a b  
    11import os
    22import re
    33from Cookie import BaseCookie, SimpleCookie, CookieError
    4 from pprint import pformat
    54from urllib import urlencode
    65from urlparse import urljoin
    76try:
    class HttpRequest(object):  
    3837        self.method = None
    3938
    4039    def __repr__(self):
    41         return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
    42             (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),
    43             pformat(self.META))
     40        try:
     41            full_path = self.get_full_path()
     42        except:
     43            full_path = "(could not parse path)"
     44        return smart_str(u'<%s: %s %s>' % (self.__class__.__name__, self.method, full_path))
    4445
    4546    def get_host(self):
    4647        """Returns the HTTP host using the environment or request headers."""
Back to Top