#16770 closed Bug (fixed)
Don't wrap exceptions in TemplateSyntaxError under DEBUG
Reported by: | jMyles | Owned by: | jMyles |
---|---|---|---|
Component: | Template system | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This problem is easy to reproduce:
*Create a new django project.
*In settings.py, set the database to postgres but with a non-existent hostname.
*Create a model (Llama) and view (home)
*In the view, pass Llama.objects.all() to the context as "llamas"
*In the template, attempt to iterate through "llamas"
Attachments (7)
Change History (14)
by , 13 years ago
Attachment: | Screenshot-1.png added |
---|
comment:1 by , 13 years ago
It looks to me like the core of the problem is template/debug.py line 79.
by , 13 years ago
Attachment: | better_operationalerror.diff added |
---|
Not a final patch per se, just a first draft to get the ball rolling. This simply causes OperationalError to be raised instead of TemplateSyntaxError.
comment:2 by , 13 years ago
On advice from Carl, I have decided to unwrap the exceptions altogether. Instead, we're going to annotate the exception, passing the node (and thus the line of the template) that was rendering when the error occurred. This way, nobody will be mystified about what part of the template is hitting the database and causing OperationalError or DatabaseError.
by , 13 years ago
Attachment: | unwrap_templatesyntaxerror.diff added |
---|
OK, this patch is feeling a little more final. I have some concerns about the fact that there seems to be no branch whatsoever that causes execution of the except block of line 79 of the template/debug. I'll get confirmation.
comment:3 by , 13 years ago
Summary: | TemplateSyntaxError is raised improperly in some cases where database connection fails → Don't wrap exceptions in TemplateSyntaxError under DEBUG |
---|---|
Triage Stage: | Unreviewed → Accepted |
jMyles: this patch is looking great! Apart from some minor stylistic things which can easily be tweaked at commit time (e.g. use double-quotes not single-quotes for delimiting docstrings, and some of those comments are probably not needed at all), I just notice one substantive issue. Now that we aren't exclusively annotating TemplateSyntaxError, but any kind of exception, there's no reason to have two separate clauses in DebugNodeList.render_node, one to catch TemplateSyntaxError and one for other exceptions. There should just be a single clause for all exceptions, and it should check to make sure the annotation doesn't already exist on the exception before adding it.
Also, I'm not sure why we need the complex multi-argument re-raise anymore - ExceptionReporter should get the traceback automatically, because its getting the original exception.
by , 13 years ago
Attachment: | unwrap_templatesyntaxerror-1.diff added |
---|
Here's the latest patch. In addition to the code and tests, this one also fixes old tests that expected TemplateSyntaxError.
comment:4 by , 13 years ago
Thanks jMyles. I've done some work on the patch, I think it's pretty close to ready. Current state here: https://github.com/carljm/django/compare/master...t16770
by , 13 years ago
Attachment: | unwrap_templatesyntaxerror-3.txt added |
---|
Another correction: The first modification wasn't intentional, it was just regression from failing to svn update.
by , 13 years ago
Attachment: | unwrap_templatesyntaxerror-3.diff added |
---|
comment:5 by , 13 years ago
The corrections I made in unwrap_templatesyntaxerror-3.diff are also in the github - it's now the authoritative version of this patch: https://github.com/carljm/django/compare/master...t16770
Shot of the actual error