Fixed #1551 -- Improved base handler to not lose track of important exception in case of exceptions within repr(). Thanks, Ned

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2667 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-04-11 03:29:01 +00:00
parent 8d70ed4b25
commit 51a6d81d72
1 changed files with 7 additions and 4 deletions

View File

@ -1,4 +1,5 @@
from django.utils import httpwrappers from django.utils import httpwrappers
import sys
class BaseHandler: class BaseHandler:
def __init__(self): def __init__(self):
@ -108,12 +109,14 @@ class BaseHandler:
if DEBUG: if DEBUG:
return self.get_technical_error_response(request) return self.get_technical_error_response(request)
else: else:
# Get the exception info now, in case another exception is thrown later.
exc_info = sys.exc_info()
subject = 'Coding error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in INTERNAL_IPS and 'internal' or 'EXTERNAL'), getattr(request, 'path', '')) subject = 'Coding error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in INTERNAL_IPS and 'internal' or 'EXTERNAL'), getattr(request, 'path', ''))
try: try:
request_repr = repr(request) request_repr = repr(request)
except: except:
request_repr = "Request repr() unavailable" request_repr = "Request repr() unavailable"
message = "%s\n\n%s" % (self._get_traceback(), request_repr) message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
mail_admins(subject, message, fail_silently=True) mail_admins(subject, message, fail_silently=True)
return self.get_friendly_error_response(request, resolver) return self.get_friendly_error_response(request, resolver)
@ -138,7 +141,7 @@ class BaseHandler:
else: else:
return debug.technical_500_response(request, *sys.exc_info()) return debug.technical_500_response(request, *sys.exc_info())
def _get_traceback(self): def _get_traceback(self, exc_info=None):
"Helper function to return the traceback as a string" "Helper function to return the traceback as a string"
import sys, traceback import traceback
return '\n'.join(traceback.format_exception(*sys.exc_info())) return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))