Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14523 -- Modified response handling so that exceptions raised …

…by process_response() in a middleware are caught and handled like any other exception. Thanks to Ivan Sagalaev for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14393 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3086b55b0e96b82a7f5f7a5c488f97ff5ed76420 1 parent c38a174
Russell Keith-Magee authored
5  django/core/handlers/base.py
@@ -120,6 +120,11 @@ def get_response(self, request):
120 120
                         view_name = callback.__class__.__name__ + '.__call__' # If it's a class
121 121
                     raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
122 122
 
  123
+                # Apply response middleware
  124
+                for middleware_method in self._response_middleware:
  125
+                    response = middleware_method(request, response)
  126
+                response = self.apply_response_fixes(request, response)
  127
+
123 128
                 return response
124 129
             except http.Http404, e:
125 130
                 logger.warning('Not Found: %s' % request.path,
5  django/core/handlers/modpython.py
@@ -215,11 +215,6 @@ def __call__(self, req):
215 215
                 response = http.HttpResponseBadRequest()
216 216
             else:
217 217
                 response = self.get_response(request)
218  
-
219  
-                # Apply response middleware
220  
-                for middleware_method in self._response_middleware:
221  
-                    response = middleware_method(request, response)
222  
-                response = self.apply_response_fixes(request, response)
223 218
         finally:
224 219
             signals.request_finished.send(sender=self.__class__)
225 220
 
5  django/core/handlers/wsgi.py
@@ -251,11 +251,6 @@ def __call__(self, environ, start_response):
251 251
                 response = http.HttpResponseBadRequest()
252 252
             else:
253 253
                 response = self.get_response(request)
254  
-
255  
-                # Apply response middleware
256  
-                for middleware_method in self._response_middleware:
257  
-                    response = middleware_method(request, response)
258  
-                response = self.apply_response_fixes(request, response)
259 254
         finally:
260 255
             signals.request_finished.send(sender=self.__class__)
261 256
 
21  tests/regressiontests/middleware_exceptions/tests.py
@@ -6,10 +6,14 @@
6 6
 class TestException(Exception):
7 7
     pass
8 8
 
9  
-class TestMiddleware(object):
  9
+class TestRequestMiddleware(object):
10 10
     def process_request(self, request):
11 11
         raise TestException('Test Exception')
12 12
 
  13
+class TestResponseMiddleware(object):
  14
+    def process_response(self, request, response):
  15
+        raise TestException('Test Exception')
  16
+
13 17
 class MiddlewareExceptionTest(TestCase):
14 18
     def setUp(self):
15 19
         self.exceptions = []
@@ -23,12 +27,11 @@ def tearDown(self):
23 27
     def _on_request_exception(self, sender, request, **kwargs):
24 28
         self.exceptions.append(sys.exc_info())
25 29
 
26  
-    def test_process_request(self):
27  
-        self.client.handler._request_middleware.insert(0, TestMiddleware().process_request)
  30
+    def _assert_exception_handled(self):
28 31
         try:
29  
-            response = self.client.get('/')
  32
+            response = self.client.get('/middleware_exceptions/')
30 33
         except TestException, e:
31  
-            # Test client indefinitely re-raises any exceptions being raised
  34
+            # Test client intentionally re-raises any exceptions being raised
32 35
             # during request handling. Hence actual testing that exception was
33 36
             # properly handled is done by relying on got_request_exception
34 37
             # signal being sent.
@@ -38,3 +41,11 @@ def test_process_request(self):
38 41
         self.assertEquals(len(self.exceptions), 1)
39 42
         exception, value, tb = self.exceptions[0]
40 43
         self.assertEquals(value.args, ('Test Exception', ))
  44
+
  45
+    def test_process_request(self):
  46
+        self.client.handler._request_middleware.insert(0, TestRequestMiddleware().process_request)
  47
+        self._assert_exception_handled()
  48
+
  49
+    def test_process_response(self):
  50
+        self.client.handler._response_middleware.insert(0, TestResponseMiddleware().process_response)
  51
+        self._assert_exception_handled()

0 notes on commit 3086b55

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