{% include %} seems to confuse the stack trace. Here is a simple example:
index.html
{% include 'more.html' %}
{{ badVariable }}
more.html
This is is an included file
It needs some lines
To throw off the trace
and when I render it:
const env = nunjucks.configure('views', { throwOnUndefined: true });
env.render('demo/index.html', {}, (err: any, res: string) => {
console.log(err);
console.log(res);
throw err;
});
I get this stack trace:
Template render error: (.../views/demo/index.html)
Template render error: (.../views/demo/more.html) [Line 3, Column 1]
attempted to output null or undefined value
But note that there is no error in more.html: The error is is in index.html.
I would hypothesize that nunjucks is remembering the last include, even after the file has been exited. That would explain many of the traces I've seen, although I am not sure it explains all of them.
Hi did you try to remove the badVariable ?
@yannou788 That solves the cause of the bad stack trace. However, it doesn't make it any easy to debug the next time that there is a bad variable causing an exception. My concern here is that the stack trace is misleading.
@rayalan for the next time if you have an similar problem check the parent file at the indicated line. Example :
Template render error: (.../views/demo/index.html)
Template render error: (.../views/demo/more.html) [Line 3, Column 1]
attempted to output null or undefined value
Check the .../views/demo/index.html at [Line 3, Column 1] and after the .../views/demo/more.html
While @yannou788's solution does point me to the correct line, I have to agree that @rayalan has a point regarding why that include is even mentioned in the stack trace - it was very much a red herring and sent me in completely the wrong direction, including eventually to this issue after much Googling
Having an incorrect stack trace is almost worse than no stack trace at all. Please consider fixing this
Yea, this is super confusing and time consuming to figure out every time an issue comes up.
Most helpful comment
@yannou788 That solves the cause of the bad stack trace. However, it doesn't make it any easy to debug the next time that there is a bad variable causing an exception. My concern here is that the stack trace is misleading.