Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#12594 closed (fixed)

Meaningless exception when DEBUG=False and resolver.urlconf_module is None.

Reported by: buriy Owned by: buriy
Component: HTTP handling Version: 1.2-alpha
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Because of resolver.resolve500() can now fail due to urlconf_module set to None in django/core/handlers/base.py,
sometimes site developer doesn't get appropriate error message.
Instead developer gets message that handler500 is not found in urlconf module.

Problem is only in handler500 lookup, everything else goes well.

How to reproduce:
set DEBUG to False, comment out ROOT_URLCONF line.

I managed to find this problem it another way, but it will take some time to figure out how settings.py can be imported without errors, but get empty dir to LazySettings.

My suggested fix:

    def handle_uncaught_exception(self, request, resolver, exc_info):
        ...
        mail_admins(subject, message, fail_silently=True)
        # Return an HttpResponse that displays a friendly error message.

        if resolver.urlconf_module is None: # FIX LINE1: you aren't getting http500!
            raise exc_info[1], None, exc_info[2] # FIX LINE2: so we'll raise it raw.

        callback, param_dict = resolver.resolve500()
        return callback(request, **param_dict)

Attachments (1)

12594.patch (3.0 KB) - added by buriy 4 years ago.
Patch with tests

Download all attachments as: .zip

Change History (10)

comment:1 in reply to: ↑ description Changed 4 years ago by buriy

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Exception Before:

Traceback (most recent call last):

  File "/Projects/django/wn2/django/core/servers/basehttp.py", line 280, in run
    self.result = application(self.environ, self.start_response)

  File "/Projects/django/wn2/django/core/servers/basehttp.py", line 672, in __call__
    return self.application(environ, start_response)

  File "/Projects/django/wn2/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "/Projects/django/wn2/django/core/handlers/base.py", line 143, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "/Projects/django/wn2/django/core/handlers/base.py", line 180, in handle_uncaught_exception
    callback, param_dict = resolver.resolve500()

  File "/Projects/django/wn2/django/core/urlresolvers.py", line 269, in resolve500
    return self._resolve_special('500')

  File "/Projects/django/wn2/django/core/urlresolvers.py", line 259, in _resolve_special
    callback = getattr(self.urlconf_module, 'handler%s' % view_type)

AttributeError: 'NoneType' object has no attribute 'handler500'

Exception after:

Traceback (most recent call last):

  File "/Projects/django/wn2/django/core/servers/basehttp.py", line 280, in run
    self.result = application(self.environ, self.start_response)

  File "/Projects/django/wn2/django/core/servers/basehttp.py", line 672, in __call__
    return self.application(environ, start_response)

  File "/Projects/django/wn2/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "/Projects/django/wn2/django/core/handlers/base.py", line 143, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "/Projects/django/wn2/django/core/handlers/base.py", line 85, in get_response
    urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)

  File "/Projects/django/wn2/django/utils/functional.py", line 273, in __getattr__
    return getattr(self._wrapped, name)

AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

comment:2 Changed 4 years ago by zimnyx

I've met similar problems, when I got some internal error (I can give you further info if it's relevant) and django tried to display error page 500.
This problem occours *only* when settings.DEBUG is False.
When DEBUG is True I see helpful error page about actual error that have occoured, not error in error displaying :-)

Here you got Traceback, when DEBUG is FALSE:

    Traceback (most recent call last):

  File "/srv/trunk/external-packages/django/core/servers/basehttp.py", line 280, in run
    self.result = application(self.environ, self.start_response)

  File "/srv/trunk/external-packages/django/core/servers/basehttp.py", line 672, in __call__
    return self.application(environ, start_response)

  File "/srv/trunk/external-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "/srv/trunk/external-packages/django/core/handlers/base.py", line 143, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "/srv/trunk/external-packages/django/core/handlers/base.py", line 178, in handle_uncaught_exception
    callback, param_dict = resolver.resolve500()

  File "/srv/trunk/external-packages/django/core/urlresolvers.py", line 268, in resolve500
    return self._resolve_special('500')

  File "/srv/trunk/external-packages/django/core/urlresolvers.py", line 258, in _resolve_special
    callback = getattr(self.urlconf_module, 'handler%s' % view_type)

AttributeError: 'NoneType' object has no attribute 'handler500'

This message says nothing about actual error.

comment:3 Changed 4 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 4 years ago by kmtracey

  • Has patch unset
  • Needs tests set

Please attach a patch that is a diff, not an inline display of a complete routine.

comment:5 Changed 4 years ago by buriy

  • Owner changed from nobody to buriy
  • Status changed from new to assigned

I attached complete routine exactly because I wanted someone to write the patch and check me, I'm not sure in all python versions my re-raising exception works correctly.
Ok, going to attach test & patch tomorrow.

Changed 4 years ago by buriy

Patch with tests

comment:6 Changed 4 years ago by buriy

  • Has patch set
  • Needs tests unset

Please check, mark RFC & commit.

comment:7 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [12854]) Fixed #12594 -- Ensured that a meaningful exception is raised when the urlconf_module is None. Thanks to buriy for the report and patch.

comment:8 Changed 4 years ago by russellm

(In [12855]) [1.1.X] Fixed #12594 -- Ensured that a meaningful exception is raised when the urlconf_module is None. Thanks to buriy for the report and patch.

Backport of r12854 from trunk.

comment:9 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.