Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #6992 -- Improved error when middleware omits HttpResponse.

Thanks guettli for the report.
  • Loading branch information...
commit 47a9347b9828ed0186d676ee0c5cadd9e6a93edb 1 parent fc31319
@aaugustin aaugustin authored
Showing with 56 additions and 0 deletions.
  1. +12 −0 django/core/handlers/base.py
  2. +44 −0 tests/middleware_exceptions/tests.py
View
12 django/core/handlers/base.py
@@ -134,6 +134,12 @@ def get_response(self, request):
if hasattr(response, 'render') and callable(response.render):
for middleware_method in self._template_response_middleware:
response = middleware_method(request, response)
+ # Complain if the template response middleware returned None (a common error).
+ if response is None:
+ raise ValueError(
+ "%s.process_template_response didn't return an "
+ "HttpResponse object. It returned None instead."
+ % (middleware_method.__self__.__class__.__name__))
response = response.render()
except http.Http404 as e:
@@ -202,6 +208,12 @@ def get_response(self, request):
# Apply response middleware, regardless of the response
for middleware_method in self._response_middleware:
response = middleware_method(request, response)
+ # Complain if the response middleware returned None (a common error).
+ if response is None:
+ raise ValueError(
+ "%s.process_response didn't return an "
+ "HttpResponse object. It returned None instead."
+ % (middleware_method.__self__.__class__.__name__))
response = self.apply_response_fixes(request, response)
except: # Any exception should be gathered and handled
signals.got_request_exception.send(sender=self.__class__, request=request)
View
44 tests/middleware_exceptions/tests.py
@@ -101,6 +101,17 @@ def process_exception(self, request, exception):
raise TestException('Test Exception Exception')
+# Sample middlewares that omit to return an HttpResonse
+class NoTemplateResponseMiddleware(TestMiddleware):
+ def process_template_response(self, request, response):
+ super(NoTemplateResponseMiddleware, self).process_template_response(request, response)
+
+
+class NoResponseMiddleware(TestMiddleware):
+ def process_response(self, request, response):
+ super(NoResponseMiddleware, self).process_response(request, response)
+
+
@override_settings(ROOT_URLCONF='middleware_exceptions.urls')
class BaseMiddlewareExceptionTest(TestCase):
@@ -775,6 +786,39 @@ def test_process_exception_bad_middleware_permission_denied(self):
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
+ def test_process_response_no_response_middleware(self):
+ pre_middleware = TestMiddleware()
+ middleware = NoResponseMiddleware()
+ post_middleware = TestMiddleware()
+ self._add_middleware(post_middleware)
+ self._add_middleware(middleware)
+ self._add_middleware(pre_middleware)
+ self.assert_exceptions_handled('/middleware_exceptions/view/', [
+ "NoResponseMiddleware.process_response didn't return an HttpResponse object. It returned None instead."
+ ],
+ ValueError())
+
+ # Check that the right middleware methods have been invoked
+ self.assert_middleware_usage(pre_middleware, True, True, False, False, False)
+ self.assert_middleware_usage(middleware, True, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, False, True, False)
+
+ def test_process_template_response_no_response_middleware(self):
+ pre_middleware = TestMiddleware()
+ middleware = NoTemplateResponseMiddleware()
+ post_middleware = TestMiddleware()
+ self._add_middleware(post_middleware)
+ self._add_middleware(middleware)
+ self._add_middleware(pre_middleware)
+ self.assert_exceptions_handled('/middleware_exceptions/template_response/', [
+ "NoTemplateResponseMiddleware.process_template_response didn't return an HttpResponse object. It returned None instead."
+ ],
+ ValueError())
+
+ # Check that the right middleware methods have been invoked
+ self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, True, False)
_missing = object()
Please sign in to comment.
Something went wrong with that request. Please try again.