-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Print Jinja error context on UndefinedError #61553
Print Jinja error context on UndefinedError #61553
Conversation
5b99401
to
19e3b06
Compare
19e3b06
to
e3afb28
Compare
@Ch3LL, the test pipeline is searching for the
|
e3afb28
to
3f13e31
Compare
This provides both the line number on which the error occurred in the Jinja template, and also several lines of context around the error in the Jinja template source. The one caveat to this change is that if paired with jinja2<2.11 it could possibly report incorrect line numbers for some errors. This was fixed in pallets/jinja#1109 Traces/errors before were vague and ambiguous: File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 387, in render_jinja_tmpl buf=tmplstr) SaltRenderError: Jinja variable 'str object' has no attribute 'items' This gives a line number and multiple lines of context: File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 387, in render_jinja_tmpl tmplstr) SaltRenderError: Jinja variable 'str object' has no attribute 'items'; line 5 --- # {{ salt.pillar.get('managed_by_salt_message', '') }} # salt template: {{ source }} {%- for section in sections %} {%- for name, config in section.items() %} <====================== [{{ name }}] {%- for line in config %} {{ line }} {%- endfor %} {%- endfor %} [...] --- Signed-off-by: Joe Groocock <jgroocock@cloudflare.com>
Add a test to reinforce that salt will provide context when `jinja2.exceptions.UndefinedError` is raised.
Some functions in salt/utils/templates.py were undocumented.
3f13e31
to
f7b0c25
Compare
Hi! I'm your friendly PR bot!You might be wondering what I'm doing commenting here on your PR. Yes, as a matter of fact, I am... I'm just here to help us improve the documentation. I can't respond to Okay... so what do you do? I detect modules that are missing docstrings or "CLI Example" on existing docstrings! So what does that have to do with my PR? I noticed that in this PR there are some files changed that have some of these Okay, what are they? Well, my favorite, is that since you were making changes here I'm hoping that If I can, then what? Well, you can either add them to this PR or add them to another PR. Either way is fine! Well... what if I can't, or don't want to? That's also fine! We appreciate all contributions to the Salt Project. If you Whatever approach you decide to take, just drop a comment here letting us know! Detected Issues (click me)Check Known Missing Docstrings...........................................Failed - hook id: invoke - duration: 1.51s - exit code: 1 Thanks again! |
This is super useful, thanks a lot! Last year I did some research on how to turn the dreaded
into (note the myobj variable name)
It turned out to be very hairy: https://gist.github.com/max-arnold/94b0abf877475f3344f6f20135c7b224 Your fix solves the problem quite nicely without introducing much complexity @Ch3LL Do you think these issues could be closed? #57852 #59243 |
@jfindlay Does it print the same error multiple times for you as well?
|
This doesn't appear to work properly: #62620 |
@OrangeDog Looking at the report in that issue, I see an attached context but it appears to be the wrong file. I've never personally seen this do the wrong thing, but I could be missing something. I would expect that if the error is from an included file then you'd see that context instead. If we could get a basic reproducer I can fix it |
@frebib turns out they're in the same file, OP is just not providing the whole file. It's only the line that's wrong. |
Sounds like a Jinja issue. Maybe they're using an old Jinja version |
|
Curious. Can we get a basic reproducer? |
What does this PR do?
Fix authored by @frebib.
All other Jinja exceptions raised in
salt/utils/templates.py
are raised with source file context except forjinja2.exceptions.UndefinedError
. This change provides both the line number on which the error occurred in the Jinja template, and also several lines of context around the error in the Jinja template source forUndefinedError
.Previous Behavior
Traces/errors before were vague and ambiguous:
New Behavior
This change gives a line number and multiple lines of context:
Merge requirements satisfied?
Commits signed with GPG?
Yes