From ca6f64a43f668749c395c8c47dbb47aa175dfe25 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 10 Jan 2010 17:35:01 +0000 Subject: [PATCH] Fixed #6094 -- Middleware exceptions are now caught by the core handler. Thanks, isagalaev git-svn-id: http://code.djangoproject.com/svn/django/trunk@12165 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/handlers/base.py | 32 ++++++++-------- django/middleware/common.py | 2 + .../middleware_exceptions/models.py | 1 + .../middleware_exceptions/tests.py | 37 +++++++++++++++++++ .../middleware_exceptions/urls.py | 8 ++++ .../middleware_exceptions/views.py | 4 ++ tests/urls.py | 3 ++ 7 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 tests/regressiontests/middleware_exceptions/models.py create mode 100644 tests/regressiontests/middleware_exceptions/tests.py create mode 100644 tests/regressiontests/middleware_exceptions/urls.py create mode 100644 tests/regressiontests/middleware_exceptions/views.py diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index f144ce4bb17..1539e795d05 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -68,24 +68,24 @@ class BaseHandler(object): from django.core import exceptions, urlresolvers from django.conf import settings - # Reset the urlconf for this thread. - urlresolvers.set_urlconf(None) - - # Apply request middleware - for middleware_method in self._request_middleware: - response = middleware_method(request) - if response: - return response - - # Get urlconf from request object, if available. Otherwise use default. - urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) - - # Set the urlconf for this thread to the one specified above. - urlresolvers.set_urlconf(urlconf) - - resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) try: try: + # Reset the urlconf for this thread. + urlresolvers.set_urlconf(None) + + # Get urlconf from request object, if available. Otherwise use default. + urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) + + # Set the urlconf for this thread to the one specified above. + urlresolvers.set_urlconf(urlconf) + resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) + + # Apply request middleware + for middleware_method in self._request_middleware: + response = middleware_method(request) + if response: + return response + callback, callback_args, callback_kwargs = resolver.resolve( request.path_info) diff --git a/django/middleware/common.py b/django/middleware/common.py index b2c97c6740a..9fbbdba63a5 100644 --- a/django/middleware/common.py +++ b/django/middleware/common.py @@ -34,6 +34,8 @@ class CommonMiddleware(object): settings.APPEND_SLASH and settings.PREPEND_WWW """ + assert False, 1 + # Check for denied User-Agents if 'HTTP_USER_AGENT' in request.META: for user_agent_regex in settings.DISALLOWED_USER_AGENTS: diff --git a/tests/regressiontests/middleware_exceptions/models.py b/tests/regressiontests/middleware_exceptions/models.py new file mode 100644 index 00000000000..137941ffaef --- /dev/null +++ b/tests/regressiontests/middleware_exceptions/models.py @@ -0,0 +1 @@ +from django.db import models diff --git a/tests/regressiontests/middleware_exceptions/tests.py b/tests/regressiontests/middleware_exceptions/tests.py new file mode 100644 index 00000000000..2f950a0c4a2 --- /dev/null +++ b/tests/regressiontests/middleware_exceptions/tests.py @@ -0,0 +1,37 @@ +import sys + +from django.test import TestCase +from django.core.signals import got_request_exception + +class RequestMiddleware(object): + def process_request(self, request): + raise Exception('Exception') + +class MiddlewareExceptionTest(TestCase): + def __init__(self, *args, **kwargs): + super(MiddlewareExceptionTest, self).__init__(*args, **kwargs) + self.exceptions = [] + got_request_exception.connect(self._on_request_exception) + + def setUp(self): + self.client.handler.load_middleware() + + def tearDown(self): + self.exceptions = [] + + def _on_request_exception(self, sender, request, **kwargs): + self.exceptions.append(sys.exc_info()) + + def test_process_request(self): + self.client.handler._request_middleware.insert(0, RequestMiddleware().process_request) + try: + response = self.client.get('/') + except: + # Test client indefinitely re-raises any exceptions being raised + # during request handling. Hence actual testing that exception was + # properly handled is done by relying on got_request_exception + # signal being sent. + pass + self.assertEquals(len(self.exceptions), 1) + exception, value, tb = self.exceptions[0] + self.assertEquals(value.args, ('Exception', )) diff --git a/tests/regressiontests/middleware_exceptions/urls.py b/tests/regressiontests/middleware_exceptions/urls.py new file mode 100644 index 00000000000..63f3ba14962 --- /dev/null +++ b/tests/regressiontests/middleware_exceptions/urls.py @@ -0,0 +1,8 @@ +# coding: utf-8 +from django.conf.urls.defaults import * + +import views + +urlpatterns = patterns('', + (r'^$', views.index), +) diff --git a/tests/regressiontests/middleware_exceptions/views.py b/tests/regressiontests/middleware_exceptions/views.py new file mode 100644 index 00000000000..e2d597d9b10 --- /dev/null +++ b/tests/regressiontests/middleware_exceptions/views.py @@ -0,0 +1,4 @@ +from django import http + +def index(request): + return http.HttpResponse('') diff --git a/tests/urls.py b/tests/urls.py index 4d123a2e8ad..01d6408c5a2 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -36,6 +36,9 @@ urlpatterns = patterns('', # conditional get views (r'condition/', include('regressiontests.conditional_processing.urls')), + # middleware exceptions tests + (r'middleware_exceptions/', include('regressiontests.middleware_exceptions.urls')), + # special headers views (r'special_headers/', include('regressiontests.special_headers.urls')), )