Code

Ticket #20752: 20752_patch.diff

File 20752_patch.diff, 3.3 KB (added by schacki, 6 months ago)
Line 
1diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py
2index 9679ae8..20a6a10 100644
3--- a/django/core/handlers/base.py
4+++ b/django/core/handlers/base.py
5@@ -191,8 +191,20 @@ class BaseHandler(object):
6 
7         except: # Handle everything else.
8             # Get the exception info now, in case another exception is thrown later.
9-            signals.got_request_exception.send(sender=self.__class__, request=request)
10-            response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
11+            exc_info = sys.exc_info()
12+            signal_responses = signals.got_request_exception.send_robust(sender=self.__class__, request=request)
13+            for signal_response in signal_responses:
14+                if isinstance(signal_response[1], Exception):
15+                    message = 'Base Handler Error %s: ' % signal_response[1]
16+                    logger.error(message+'%s', request.path,
17+                        exc_info=exc_info,
18+                        extra={
19+                            'status_code': 500,
20+                            'request': request,
21+                            'error': signal_response[1]
22+                        }
23+                    )
24+            response = self.handle_uncaught_exception(request, resolver, exc_info)
25 
26         try:
27             # Apply response middleware, regardless of the response
28diff --git a/tests/view_tests/tests/test_defaults.py b/tests/view_tests/tests/test_defaults.py
29index d55ed53..7a74b0b 100644
30--- a/tests/view_tests/tests/test_defaults.py
31+++ b/tests/view_tests/tests/test_defaults.py
32@@ -1,9 +1,16 @@
33 from __future__ import unicode_literals
34 
35+import logging
36+
37+from django.utils.six import StringIO
38+
39+from django.core.signals import got_request_exception
40+from django.dispatch import receiver
41 from django.contrib.contenttypes.models import ContentType
42+
43 from django.test import TestCase
44 from django.test.utils import (setup_test_template_loader,
45-    restore_template_loaders, override_settings)
46+    restore_template_loaders, override_settings, patch_logger)
47 
48 from ..models import Author, Article, UrlArticle
49 
50@@ -33,8 +40,27 @@ class DefaultsTests(TestCase):
51 
52     def test_server_error(self):
53         "The server_error view raises a 500 status"
54-        response = self.client.get('/views/server_error/')
55-        self.assertEqual(response.status_code, 500)
56+        with self.settings(DEBUG=True):
57+            with patch_logger('django.request', 'error') as calls:
58+                response = self.client.get('/views/server_error/')
59+                self.assertEqual(response.status_code, 500)
60+                self.assertEqual(len(calls), 1)       
61+
62+    def test_server_error_with_handler_error(self):
63+        """
64+        The server_error view raises a 500 status and while handling
65+        the error, the next error is raised.
66+        """
67+
68+        @receiver(got_request_exception)
69+        def handler(sender, **kwargs):
70+            raise Exception('I am a Handler Error')
71+
72+        with self.settings(DEBUG=True):
73+            with patch_logger('django.request', 'error') as calls:
74+                response = self.client.get('/views/server_error/')
75+                self.assertEqual(response.status_code, 500)
76+                self.assertEqual(len(calls), 2)
77 
78     def test_custom_templates(self):
79         """