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

Enhance Default Exceptions to include Line Number. #1861

Open
rubengm13 opened this issue Jun 15, 2023 · 1 comment
Open

Enhance Default Exceptions to include Line Number. #1861

rubengm13 opened this issue Jun 15, 2023 · 1 comment

Comments

@rubengm13
Copy link

When a Built-in Exception is raised, the raised Exception should be modified such that it includes the line number of the Template Line violating the exception.

This Example will trigger a TypeError.

import jinja2
environment = jinja2.Environment()
jinja_template = """
    {%- for name in names %}
        ("HELLO {{name}}!")
    {%- endfor %}
"""
template = environment.from_string(jinja_template)
data = template.render(names=None)
print(data)

Ideally, if we could modify the triggered TypeError Exception to add a lineno attribute so that we can capture the lineno to better track the line number that triggered the issue.

import jinja2
environment = jinja2.Environment()
jinja_template = """
    {%- for name in names %}
        ("HELLO {{name}}!")
    {%- endfor %}
"""
try:
    template = environment.from_string(jinja_template)
    data = template.render(names=None)
except Exception as e:
    print(f"Error on Line Number: {e.lineno}")
print(data)
@davidism
Copy link
Member

Happy to review a PR

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