mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #14664 -- Logged a warning if MiddlewareNotUsed is raised in DEBUG mode.
This commit is contained in:
parent
bd337184f1
commit
093e6c68b9
@ -49,7 +49,12 @@ class BaseHandler(object):
|
||||
mw_class = import_string(middleware_path)
|
||||
try:
|
||||
mw_instance = mw_class()
|
||||
except MiddlewareNotUsed:
|
||||
except MiddlewareNotUsed as exc:
|
||||
if settings.DEBUG:
|
||||
if six.text_type(exc):
|
||||
logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc)
|
||||
else:
|
||||
logger.debug('MiddlewareNotUsed: %r', middleware_path)
|
||||
continue
|
||||
|
||||
if hasattr(mw_instance, 'process_request'):
|
||||
|
@ -337,6 +337,10 @@ Middleware
|
||||
<django.middleware.common.CommonMiddleware.response_redirect_class>`
|
||||
attribute allows you to customize the redirects issued by the middleware.
|
||||
|
||||
* A debug message will be logged to the ``django.request`` logger when a
|
||||
middleware raises a :exc:`~django.core.exceptions.MiddlewareNotUsed` exception
|
||||
in :setting:`DEBUG` mode.
|
||||
|
||||
Migrations
|
||||
^^^^^^^^^^
|
||||
|
||||
|
@ -269,7 +269,14 @@ Marking middleware as unused
|
||||
It's sometimes useful to determine at run-time whether a piece of middleware
|
||||
should be used. In these cases, your middleware's ``__init__`` method may
|
||||
raise :exc:`django.core.exceptions.MiddlewareNotUsed`. Django will then remove
|
||||
that piece of middleware from the middleware process.
|
||||
that piece of middleware from the middleware process and a debug message will
|
||||
be logged to the ``django.request`` logger when :setting:`DEBUG` is set to
|
||||
``True``.
|
||||
|
||||
.. versionchanged:: 1.8
|
||||
|
||||
Previously, :exc:`~django.core.exceptions.MiddlewareNotUsed` exceptions
|
||||
weren't logged.
|
||||
|
||||
Guidelines
|
||||
----------
|
||||
|
@ -1,11 +1,13 @@
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import MiddlewareNotUsed
|
||||
from django.core.signals import got_request_exception
|
||||
from django.http import HttpResponse
|
||||
from django.template.response import TemplateResponse
|
||||
from django.template import Template
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test import RequestFactory, TestCase, override_settings
|
||||
from django.test.utils import patch_logger
|
||||
|
||||
|
||||
class TestException(Exception):
|
||||
@ -832,3 +834,65 @@ class RootUrlconfTests(TestCase):
|
||||
# the previously defined settings.
|
||||
del settings.ROOT_URLCONF
|
||||
self.assertRaises(AttributeError, self.client.get, "/middleware_exceptions/view/")
|
||||
|
||||
|
||||
class MyMiddleware(object):
|
||||
|
||||
def __init__(self):
|
||||
raise MiddlewareNotUsed
|
||||
|
||||
def process_request(self, request):
|
||||
pass
|
||||
|
||||
|
||||
class MyMiddlewareWithExceptionMessage(object):
|
||||
|
||||
def __init__(self):
|
||||
raise MiddlewareNotUsed('spam eggs')
|
||||
|
||||
def process_request(self, request):
|
||||
pass
|
||||
|
||||
|
||||
@override_settings(
|
||||
DEBUG=True,
|
||||
ROOT_URLCONF='middleware_exceptions.urls',
|
||||
)
|
||||
class MiddlewareNotUsedTests(TestCase):
|
||||
|
||||
rf = RequestFactory()
|
||||
|
||||
def test_raise_exception(self):
|
||||
request = self.rf.get('middleware_exceptions/view/')
|
||||
with self.assertRaises(MiddlewareNotUsed):
|
||||
MyMiddleware().process_request(request)
|
||||
|
||||
@override_settings(MIDDLEWARE_CLASSES=(
|
||||
'middleware_exceptions.tests.MyMiddleware',
|
||||
))
|
||||
def test_log(self):
|
||||
with patch_logger('django.request', 'debug') as calls:
|
||||
self.client.get('/middleware_exceptions/view/')
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertEqual(
|
||||
calls[0],
|
||||
"MiddlewareNotUsed: 'middleware_exceptions.tests.MyMiddleware'"
|
||||
)
|
||||
|
||||
@override_settings(MIDDLEWARE_CLASSES=(
|
||||
'middleware_exceptions.tests.MyMiddlewareWithExceptionMessage',
|
||||
))
|
||||
def test_log_custom_message(self):
|
||||
with patch_logger('django.request', 'debug') as calls:
|
||||
self.client.get('/middleware_exceptions/view/')
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertEqual(
|
||||
calls[0],
|
||||
"MiddlewareNotUsed('middleware_exceptions.tests.MyMiddlewareWithExceptionMessage'): spam eggs"
|
||||
)
|
||||
|
||||
@override_settings(DEBUG=False)
|
||||
def test_do_not_log_when_debug_is_false(self):
|
||||
with patch_logger('django.request', 'debug') as calls:
|
||||
self.client.get('/middleware_exceptions/view/')
|
||||
self.assertEqual(len(calls), 0)
|
||||
|
Loading…
Reference in New Issue
Block a user