Author: mtredinnick
Date: 2008-07-15 14:17:49 -0500 (Tue, 15 Jul 2008)
New Revision: 7928

Modified:
   django/trunk/django/core/handlers/base.py
Log:
Refactored the HTTP 500 error response creation slightly. Provides the ability
for subclassing that piece of the processing path.


Modified: django/trunk/django/core/handlers/base.py
===================================================================
--- django/trunk/django/core/handlers/base.py   2008-07-15 18:47:49 UTC (rev 
7927)
+++ django/trunk/django/core/handlers/base.py   2008-07-15 19:17:49 UTC (rev 
7928)
@@ -60,7 +60,6 @@
     def get_response(self, request):
         "Returns an HttpResponse object for the given HttpRequest"
         from django.core import exceptions, urlresolvers
-        from django.core.mail import mail_admins
         from django.conf import settings
 
         # Apply request middleware
@@ -122,22 +121,37 @@
 
             if settings.DEBUG_PROPAGATE_EXCEPTIONS:
                 raise
-            elif settings.DEBUG:
-                from django.views import debug
-                return debug.technical_500_response(request, *exc_info)
-            else:
-                # When DEBUG is False, send an error message to the admins.
-                subject = 'Error (%s IP): %s' % 
((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 
'EXTERNAL'), request.path)
-                try:
-                    request_repr = repr(request)
-                except:
-                    request_repr = "Request repr() unavailable"
-                message = "%s\n\n%s" % (self._get_traceback(exc_info), 
request_repr)
-                mail_admins(subject, message, fail_silently=True)
-                # Return an HttpResponse that displays a friendly error 
message.
-                callback, param_dict = resolver.resolve500()
-                return callback(request, **param_dict)
+            return self.handle_uncaught_exception(request, resolver, exc_info)
 
+    def handle_uncaught_exception(self, request, resolver, exc_info):
+        """
+        Processing for any otherwise uncaught exceptions (those that will
+        generate HTTP 500 responses). Can be overridden by subclasses who want
+        customised 500 handling.
+
+        Be *very* careful when overriding this because the error could be
+        caused by anything, so assuming something like the database is always
+        available would be an error.
+        """
+        from django.conf import settings
+        from django.core.mail import mail_admins
+
+        if settings.DEBUG:
+            from django.views import debug
+            return debug.technical_500_response(request, *exc_info)
+
+        # When DEBUG is False, send an error message to the admins.
+        subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in 
settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
+        try:
+            request_repr = repr(request)
+        except:
+            request_repr = "Request repr() unavailable"
+        message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr)
+        mail_admins(subject, message, fail_silently=True)
+        # Return an HttpResponse that displays a friendly error message.
+        callback, param_dict = resolver.resolve500()
+        return callback(request, **param_dict)
+
     def _get_traceback(self, exc_info=None):
         "Helper function to return the traceback as a string"
         import traceback


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to