Opened 19 years ago

Closed 18 years ago

#1647 closed defect (fixed)

django/core/template/__init__.py destroys useful information about exception

Reported by: alankila Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The DebugNodeList hides useful information about the exception that occured.

class DebugNodeList(NodeList):
    def render_node(self, node, context):
        try:
            result = node.render(context)
        except TemplateSyntaxError, e:
            if not hasattr(e, 'source'):
                e.source = node.source
            raise
        except Exception:
            from sys import exc_info
            wrapped = TemplateSyntaxError('Caught an exception while rendering.')
            wrapped.source = node.source
            wrapped.exc_info = exc_info()
            raise wrapped
        return result

I propose changing it like this:

class DebugNodeList(NodeList):
    def render_node(self, node, context):
        try:
            result = node.render(context)
        except TemplateSyntaxError, e:
            if not hasattr(e, 'source'):
                e.source = node.source
            raise
        except Exception e:
            from sys import exc_info
            wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % e)
            wrapped.source = node.source
            wrapped.exc_info = exc_info()
            raise wrapped
        return result

I had a bug that caused the stock 500 handler to fail to work. The change makes the difference of debuggability between the errors:

TemplateSyntaxError: Caught an exception while rendering.

and

TemplateSyntaxError: Caught an exception while rendering: 'Attachment' object has no attribute 'message'.

After this change, I was able to make admin work again. It appears that my model's repr function had hit a problem or limitation with Django. (Separate ticket about the limitation follows.)

Change History (3)

comment:1 by chphilli+django@…, 19 years ago

Thanks for this fix! It helped me figure out a huge problem I didn't understand!

comment:2 by chukharev@…, 18 years ago

Thanks a lot from me too! This simplifies debugging a LOT.

One minor correction. A comma is needed in the proposed change:

<         except Exception e:
>         except Exception, e:

comment:3 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

(In [3375]) Fixed #1647 -- Included slightly more information in template syntax error
displays in DebugNodeList.

Note: See TracTickets for help on using tickets.
Back to Top