Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13090 -- Corrected handling of errors in middleware when DEBUG…

…=False. Thanks to EroSennin for the report, and Ivan Sagalaev for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12773 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 794690c272fd7e9ee00242980e461214fcd80fcf 1 parent 4120a18
Russell Keith-Magee authored March 12, 2010
19  django/core/handlers/base.py
@@ -70,10 +70,10 @@ def get_response(self, request):
70 70
 
71 71
         try:
72 72
             try:
73  
-                # Reset the urlconf for this thread.
74  
-                urlresolvers.set_urlconf(None)
75  
-                # Obtain a default resolver. It's needed early for handling 404's.
76  
-                resolver = urlresolvers.RegexURLResolver(r'^/', None)
  73
+                # Setup default url resolver for this thread.
  74
+                urlconf = settings.ROOT_URLCONF
  75
+                urlresolvers.set_urlconf(urlconf)
  76
+                resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
77 77
 
78 78
                 # Apply request middleware
79 79
                 for middleware_method in self._request_middleware:
@@ -81,12 +81,11 @@ def get_response(self, request):
81 81
                     if response:
82 82
                         return response
83 83
 
84  
-                # Get urlconf from request object, if available.  Otherwise use default.
85  
-                urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
86  
-                # Set the urlconf for this thread to the one specified above.
87  
-                urlresolvers.set_urlconf(urlconf)
88  
-                # Reset the resolver with a possibly new urlconf
89  
-                resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
  84
+                if hasattr(request, "urlconf"):
  85
+                    # Reset url resolver with a custom urlconf.
  86
+                    urlconf = request.urlconf
  87
+                    urlresolvers.set_urlconf(urlconf)
  88
+                    resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
90 89
 
91 90
                 callback, callback_args, callback_kwargs = resolver.resolve(
92 91
                         request.path_info)
15  tests/regressiontests/middleware_exceptions/tests.py
@@ -3,9 +3,12 @@
3 3
 from django.test import TestCase
4 4
 from django.core.signals import got_request_exception
5 5
 
6  
-class RequestMiddleware(object):
  6
+class TestException(Exception):
  7
+    pass
  8
+
  9
+class TestMiddleware(object):
7 10
     def process_request(self, request):
8  
-        raise Exception('Exception')
  11
+        raise TestException('Test Exception')
9 12
 
10 13
 class MiddlewareExceptionTest(TestCase):
11 14
     def setUp(self):
@@ -21,15 +24,17 @@ def _on_request_exception(self, sender, request, **kwargs):
21 24
         self.exceptions.append(sys.exc_info())
22 25
 
23 26
     def test_process_request(self):
24  
-        self.client.handler._request_middleware.insert(0, RequestMiddleware().process_request)
  27
+        self.client.handler._request_middleware.insert(0, TestMiddleware().process_request)
25 28
         try:
26 29
             response = self.client.get('/')
27  
-        except:
  30
+        except TestException, e:
28 31
             # Test client indefinitely re-raises any exceptions being raised
29 32
             # during request handling. Hence actual testing that exception was
30 33
             # properly handled is done by relying on got_request_exception
31 34
             # signal being sent.
32 35
             pass
  36
+        except Exception, e:
  37
+            self.fail("Unexpected exception: %s" % e)
33 38
         self.assertEquals(len(self.exceptions), 1)
34 39
         exception, value, tb = self.exceptions[0]
35  
-        self.assertEquals(value.args, ('Exception', ))
  40
+        self.assertEquals(value.args, ('Test Exception', ))

0 notes on commit 794690c

Please sign in to comment.
Something went wrong with that request. Please try again.