#13480: (patch) Gracefully handling ImportErrors in user modules.
----------------------------+-----------------------------------------------
 Reporter:  FunnyMan3595    |       Owner:  nobody    
   Status:  new             |   Milestone:            
Component:  Core framework  |     Version:  1.0       
 Keywords:                  |       Stage:  Unreviewed
Has_patch:  1               |  
----------------------------+-----------------------------------------------
 (This issue is filed against 1.0 because that's the version I have
 installed (1.0.2, specifically) and can confirm it with.  Code inspection
 suggests that the problem still exists in SVN, and the attached patch
 applies to SVN successfully, albeit with some grumbling about the line
 numbers being wrong.)

 Consider the following line of code, taken from a Django application's
 views.py file:

 from django.contrib.models.auth import User

 This represents a fairly common type of programmer error, a mistake in
 naming.  In this case, I attempted to import models.auth instead of the
 correct auth.models.  As expected, this generates an error which must be
 fixed.

 If this error happens within a view function, Django will catch it and (if
 DEBUG is on) print a nice error message.

 If, however, this error happens at the module level, Django fails to catch
 the error, and it becomes a generic 500 error.  The exception falls
 through to the webserver's error logs instead of being instantly displayed
 to the programmer.

 The reason for this distinction is that at the module level, any exception
 is transformed into an ImportError, making the module completely
 unavailable and propagating the exception instantly.  This causes Django
 to fail before it has entered the usual error-catching mechanism.

 The attached patch fixes this by wrapping the code which triggers the
 initial import in the same error-catching routines, and also alters the
 point of import so that the original ImportError may be displayed instead
 of the ViewDoesNotExist exception which Django raises in its stead.

 This patch should be considered a proof-of-concept.  It has not been
 polished beyond basic debugging, and may well be implemented at a
 suboptimal level of the initial traceback or written in a non-idiomatic
 fashion.  It may also be worth searching for other places where a similar
 treatment is necessary, but this is beyond my current understanding of
 Django.

 The patch does, however, solve my problem.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13480>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to