[Django] #27827: Raising InvalidTemplateLibrary completely masks out real exception in get_package_libraries

43 views
Skip to first unread message

Django

unread,
Feb 10, 2017, 2:27:42 PM2/10/17
to django-...@googlegroups.com
#27827: Raising InvalidTemplateLibrary completely masks out real exception in
get_package_libraries
-------------------------------------+-------------------------------------
Reporter: | Owner: (none)
kehindeadewusi |
Type: | Status: new
Cleanup/optimization |
Component: Error | Version: 1.10
reporting |
Severity: Normal | Keywords: debugging
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
**Summary**
In django/template/backends/django.py, function get_package_libraries on
line 119 completely masks out the ImportError and raises
InvalidTemplateLibrary. This makes it incredibly difficult to debug
application issues.
Probably better not to handle the exception in the first place since it
only raises another type and inner exception looses the stack trace.

**To reproduce**
Create two apps e.g. form_utils and reports.
Write a template tag in reports e.g. reports.templatetags.report_tags.
(reports/templatetags/report_tags.py
Add a simple module in form_utils e.g. widgets.py.
In widgets.py, import a none-existent module e.g. from django.forms.util
import flatatt (was removed in > django 1.4)
import form_utils.widget in report_tags e.g. from form_utils.widgets
import CalendarWidget
A quick way to reproduce the error would be to register some models with
admin and navigate to /admin

The following error will be raised in get_package_libraries:

InvalidTemplateLibrary at /admin/login/

Invalid template library specified. ImportError raised when trying to load
'reports.templatetags.report_tags': No module named util

Request Method: GET
Request URL: http://localhost:2017/admin/login/?next=/admin/
Django Version: 1.10.1
Exception Type: InvalidTemplateLibrary
Exception Value:
Invalid template library specified. ImportError raised when trying to load
'reports.templatetags.report_tags': No module named util
Exception Location: D:\repo\django110\lib\site-
packages\django\template\backends\django.py in get_package_libraries, line
130

**However, if the exception was not caught and "wrongly" re-raised as an
InvalidTemplateLibrary, the following errors would be printed:**

ImportError at /admin/login/
No module named util
Request Method: GET
Request URL: http://localhost:2017/admin/login/?next=/admin/
Django Version: 1.10.1
Exception Type: ImportError
Exception Value:
No module named util
Exception Location:
D:\repo\projects\evincehr\apps\form_utils\widgets.py in <module>, line 3

**The second behavior is more appropriate to debugging the error and the
error would be quickly found.**

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

Django

unread,
Feb 10, 2017, 2:55:02 PM2/10/17
to django-...@googlegroups.com
#27827: Raising InvalidTemplateLibrary completely masks out real exception in
get_package_libraries
-------------------------------------+-------------------------------------
Reporter: kehindeadewusi | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Error reporting | Version: 1.10
Severity: Normal | Resolution:
Keywords: debugging | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by kehindeadewusi):

* Attachment "bad_error_message.png" added.

Invalid error message.

Django

unread,
Feb 10, 2017, 2:57:16 PM2/10/17
to django-...@googlegroups.com
#27827: Raising InvalidTemplateLibrary completely masks out real exception in
get_package_libraries
-------------------------------------+-------------------------------------
Reporter: kehindeadewusi | Owner: (none)
Type: | Status: new
Cleanup/optimization |

Component: Error reporting | Version: 1.10
Severity: Normal | Resolution:
Keywords: debugging | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by kehindeadewusi):

* Attachment "good_error_message.png" added.

good error message.

Django

unread,
Feb 10, 2017, 3:00:40 PM2/10/17
to django-...@googlegroups.com
#27827: Raising InvalidTemplateLibrary completely masks out real exception in
get_package_libraries
--------------------------------------+------------------------------------
Reporter: Kehinde Adewusi | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Template system | Version: 1.10
Severity: Normal | Resolution:
Keywords: debugging | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* owner: (none) => nobody
* component: Error reporting => Template system
* stage: Unreviewed => Accepted


Comment:

I'm not sure if the "helpful" message added in
655f52491505932ef04264de2bce21a03f3a7cd0 must be removed, but since master
only supports Python 3, there's an opportunity to use Python 3 exception
chaining, e.g. `raise InvalidTemplateLibrary(...) from e`.

--
Ticket URL: <https://code.djangoproject.com/ticket/27827#comment:1>

Django

unread,
Dec 27, 2020, 10:27:55 AM12/27/20
to django-...@googlegroups.com
#27827: Raising InvalidTemplateLibrary completely masks out real exception in
get_package_libraries
-------------------------------------+-------------------------------------
Reporter: Kehinde Adewusi | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: assigned
Component: Template system | Version: master

Severity: Normal | Resolution:
Keywords: debugging | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* owner: nobody => Jacob Walls
* status: new => assigned
* has_patch: 0 => 1
* version: 1.10 => master


Comment:

This PR re-raises the exception from the original instance of an
`ImportError`.

[https://github.com/django/django/pull/13814 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/27827#comment:2>

Django

unread,
Dec 28, 2020, 2:56:32 AM12/28/20
to django-...@googlegroups.com
#27827: Raising InvalidTemplateLibrary completely masks out real exception in
get_package_libraries
-------------------------------------+-------------------------------------
Reporter: Kehinde Adewusi | Owner: Jacob
Type: | Walls
Cleanup/optimization | Status: closed

Component: Template system | Version: master
Severity: Normal | Resolution: fixed

Keywords: debugging | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"89fc144dedc737a79929231438f035b1d4a993c9" 89fc144]:
{{{
#!CommitTicketReference repository=""
revision="89fc144dedc737a79929231438f035b1d4a993c9"
Fixed #27827 -- Used "raise from" when raising InvalidTemplateLibrary
exceptions in get_package_libraries().

This change sets the __cause__ attribute to raised exceptions and makes
small cleanups in error messages.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27827#comment:3>

Reply all
Reply to author
Forward
0 new messages