Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception Handling when using jinja2.DictLoader #2065

Open
cmason3 opened this issue Dec 21, 2024 · 2 comments
Open

Exception Handling when using jinja2.DictLoader #2065

cmason3 opened this issue Dec 21, 2024 · 2 comments

Comments

@cmason3
Copy link

cmason3 commented Dec 21, 2024

If I use the jinja2.FileSystemLoader and there is an error within my Jinja2 template then the Exception is raised and it reports the file that caused the issue, e.g:

  File "test.j2", line 1, in template
    Test {% x
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'x'.

However, if I use jinja2.DictLoader then when it raises an Exception it reports the file as <unknown> even though I am passing it the name of a template, e.g:

  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'x'.

The following code replicates the issue:

#!/usr/bin/env python3

import jinja2, traceback

template = {
  'Default': '{% Test x'
}

try:
  env = jinja2.Environment(loader=jinja2.DictLoader(template))
  rtemplate = env.get_template('Default')
  content = rtemplate.render()

except Exception:
  traceback.print_exc()

The full trackback is as follows:

Traceback (most recent call last):
  File "/home/cmason3/./test.py", line 11, in <module>
    rtemplate = env.get_template('Default')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cmason3/bin/python3/lib/python3.11/site-packages/jinja2/environment.py", line 1013, in get_template
    return self._load_template(name, globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cmason3/bin/python3/lib/python3.11/site-packages/jinja2/environment.py", line 972, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cmason3/bin/python3/lib/python3.11/site-packages/jinja2/loaders.py", line 138, in load
    code = environment.compile(source, name, filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cmason3/bin/python3/lib/python3.11/site-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/home/cmason3/bin/python3/lib/python3.11/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'Test'.

As we are passing the name of the template into get_template() then it knows the name of the template - the same is true if it includes another template from the dict - is it possible to actually report the template name (instead of <unknown> - maybe <actual template name>) when using the DictLoader? When I include multiple templates it gets quite confusing to understand which template the error occurred in - the line numbers seem to be correct once I work out what template it is referring to.

Environment:

  • Python version: 3.11.2
  • Jinja version: 3.1.4
@davidism
Copy link
Member

The value in the traceback is the name of a file, not the name of a template. There's no file backing templates for DictLoader, only template names.

@cmason3
Copy link
Author

cmason3 commented Dec 21, 2024

Is there no way to add the name of the template into the traceback else the user has no idea which one of their templates caused the error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants