Opened 11 years ago

Closed 9 years ago

#1421 closed enhancement (wontfix)

Enable AttributeError reporting in template rendering

Reported by: Nebojsa Djordjevic <nesh at studioquattro dot co dot yu> Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: nesh@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This bugs me a while.

When exception is thrown inside model function not traceback or any indication is given (with DEBUG=True), for example
(m-r):

class Foo(models.Model)
    def bar(self):
        raise SomeException()

will return nothing (None) with *no indication* that error is occurred! Which leads to mysterious errors or missing data
without any indication what is happened.

So I got accustomed to write something like this:

class Foo(models.Model)
    def bar(self):
        try:
                raise SomeException()
        except Exception, err:
                return str(err)

to, at least, get some indication (as a text returned from function) when error is occurred.

Any solutions or plans for this?

Change History (6)

comment:1 Changed 11 years ago by Adrian Holovaty

I cannot reproduce this. In my test, the SomeException exception was raised just fine. I tested that on magic-removal.

Perhaps you're dealing with the template system here? The template system automatically catches exceptions when rendering content...

comment:2 Changed 11 years ago by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

Summary: Exceptions inside model functionsExceptions inside functions called from templates

I hit this with get_admin_url in the LogEntry #1417. Instead of raising attribute error it's silently fails.

example (m-r):

def get_admin_url(self):
    """
    Returns the admin URL to edit the object represented by this log entry.
    This is relative to the Django admin index page.
    """
    try:
        return "%s/%s/%s/" % (self.content_type.app_label1, self.content_type.model, self.object_id)
    except Exception, err:
        return str(err)

(note self.content_type.app_label1)

If I don't use try..except block I only get empty href's in admin instead od AttributeError.

Perhaps you're dealing with the template system here? The template system automatically catches exceptions when rendering content...

If I understood corrrectly, any exception which is raised inside template rendering like <a href="{{ entry.get_admin_url }}"> will be silently ignored?

This type of behavior can produce some hard to find errors which will be trivial to fix if exception is reported.

comment:3 Changed 11 years ago by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

Component: Core frameworkTemplate system
Summary: Exceptions inside functions called from templatesEnable AttributeError reporting in template rendering

Update:

It's seems to be a problem only with AttributeError, other exceptions are reported.

IIRC template system ignores AttributeError and treat them as a blank (which is a Good Thing), but in cases like this it can be very confusing.

Maybe we can add some flag which will stop ignoring AttributeError in template code for debugging purporses?

comment:4 Changed 10 years ago by Michael Radziej <mir@…>

Triage Stage: UnreviewedDesign decision needed

comment:5 Changed 9 years ago by Nebojša Đorđević <nesh@…>

Cc: nesh@… added; nesh@… removed

comment:6 Changed 9 years ago by Malcolm Tredinnick

Resolution: wontfix
Status: newclosed

There are lots of places in Django that rely on AttributeError being handled as it is. If you want to try and debug particular cases, set TEMPLATE_STRING_IF_INVALID to something non-empty. There's not much else we can do here.

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