Code

Opened 8 years ago

Closed 7 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
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?

Attachments (0)

Change History (6)

comment:1 Changed 8 years ago by adrian

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 8 years ago by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

  • Summary changed from Exceptions inside model functions to Exceptions 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 8 years ago by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

  • Component changed from Core framework to Template system
  • Summary changed from Exceptions inside functions called from templates to Enable 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 7 years ago by Michael Radziej <mir@…>

  • Triage Stage changed from Unreviewed to Design decision needed

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

  • Cc nesh@… added; nesh@… removed

comment:6 Changed 7 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.