Opened 9 years ago

Closed 8 years ago

#26573 closed Cleanup/optimization (fixed)

Empty 'AssertionError' exception message on syntax error ({% else if %}) in templates

Reported by: Vasili Korol Owned by: nobody
Component: Template system Version: 1.8
Severity: Normal Keywords: assertionerror template elsif
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

If one erroneously uses {% else if ... %} instead of {% elif ... %} in django templates:

{% if task.status == TASK_STATUS_DONE %}                                           
    ...                                              
{% else if task.status == TASK_STATUS_ERROR or task.status == TASK_STATUS_ABORTED %}
    ...                                            
{% endif %}                                                                        

then the following error without any message emerges:

AssertionError at /wizard/tasks/1/summary
No exception message supplied

Environment:

Request Method: GET
Request URL: http://127.0.0.1:20090/wizard/tasks/1/summary

Django Version: 1.8.13
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'bootstrap3',
 'djkombu',
  ...)
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'viking.middleware.LoginRequiredMiddleware')


Traceback:
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.4/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)
File "/usr/local/lib/python3.4/dist-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/rest_framework/decorators.py" in handler
  53.             return func(*args, **kwargs)
File "/www/deploy/apps/wizard/views.py" in task_id_step
  156.         return render_to_response( "wizard/step_summary.html", template_data )
File "/usr/local/lib/python3.4/dist-packages/django/shortcuts.py" in render_to_response
  39.         content = loader.render_to_string(template_name, context, using=using)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in render_to_string
  98.             template = get_template(template_name, using=using)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in get_template
  35.                 return engine.get_template(template_name, dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/backends/django.py" in get_template
  30.         return Template(self.engine.get_template(template_name, dirs))
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py" in get_template
  167.         template, origin = self.find_template(template_name, dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py" in find_template
  141.                 source, display_name = loader(name, dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/loaders/base.py" in __call__
  13.         return self.load_template(template_name, template_dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/loaders/base.py" in load_template
  23.             template = Template(source, origin, template_name, self.engine)
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in __init__
  191.         self.nodelist = engine.compile_string(template_string, origin)
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py" in compile_string
  261.         return parser.parse()
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in parse
  342.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader_tags.py" in do_extends
  210.     nodelist = parser.parse()
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in parse
  342.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader_tags.py" in do_block
  184.     nodelist = parser.parse(('endblock',))
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in parse
  342.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.4/dist-packages/django/template/defaulttags.py" in do_if
  1033.     assert token.contents == 'endif'

Exception Type: AssertionError at /wizard/tasks/1/summary
Exception Value: 

I think it would be nice to include some meaningful message with the exception.

Change History (7)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 by Quentin Fulsher, 9 years ago

Would a message like this work here:

'Malformed template tag at line {0}: "{1}"'.format(token.lineno, token.contents)

In this case the message would look something like:

'Malformed template tag at line 35: "else if task.status == TASK_STATUS_ERROR or task.status == TASK_STATUS_ABORTED" 

in reply to:  2 comment:3 by Vasili Korol, 9 years ago

Replying to inondle:

Yes, i think this will work perfectly.

comment:4 by Quentin Fulsher, 9 years ago

Cool, here's my pull request:

https://github.com/django/django/pull/6602

comment:5 by Tim Graham, 9 years ago

Has patch: set

For future reference, please check "Has patch" so the ticket appears in the review queue.

comment:6 by Tim Graham, 9 years ago

Patch needs improvement: set

As noted on the PR, the debug view doesn't highlight the correct line in this case. I think it'd be worth looking into whether this could be fixed.

comment:7 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 93c312c:

Fixed #26573 -- Added descriptive error message for malformed if/else/elif template tags.

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