Nunjucks: Errors seem to be swallowed.

Created on 13 Jul 2018  路  12Comments  路  Source: mozilla/nunjucks

I'm new to Nunjucks so please forgive me but I'm finding it very hard to debug when things start going wrong as errors don't seem to propagate up.

Whether it's a syntax error in the template, or a missing template, no errors are raised and and null is returned from the template.render(context) method.

My render pipeline is sync, the only way I've been able to get it to report the error though is to provide an error-first callback to the render method, get the error, then remember to remove the callback after. This is quite frustrating.

I've also tried setting the throwOnUndefined option to true but that doesn't seem to make a difference.

Can anyone shed a light on this? I can provide some example code if needed.

While this is proving to be painful from a dev point of view, my bigger concern is that errors are swallowed in production.

Many thanks

Dave

bug

Most helpful comment

Looks like this could be related to #678

All 12 comments

Can you provide the example illustrating the issue? Thanks!

Hi @devoidfury.

Here's is an example of missing import.
I would expect the following to throw, given the file import doesn't exist.

No errors are thrown, the result html is null. throwOnUndefined doesn't have any affect.

const nunjucks = require('nunjucks')

const env = nunjucks.configure('./', {
  autoescape: true,
  watch: false,
  // throwOnUndefined: true
})

const template = nunjucks.compile(`
{% from "bad-path/macro.njk" import foo %}
{{title}}
`, env)

const html = template.render({title: 'Hello world'})

console.log(html)
==> null

If I change the render to async, I do get an error in the callback:

template.render({title: 'Hello world'}, function (err, html) {
  console.error(err)
  ==> Template render error: (unknown path)
  console.log(html)
})

I hope this is clear, many thanks

I am seeing this with {% include %} as well and I don't know what the problem is. Without any errors, I can't tell what the problem is, and the HTML coming back is null.

This also happens for throwOnUndefined = true when the template is syntactically correct and all referenced files are in place, but the context is missing a variable that the template references. The render output is null, but nothing is actually thrown in the synchronous API. As with the above, the error can be captured in a callback function.

Looks like this could be related to #678

Sorry to revive an old post, but I started using Nunjucks some days ago and the problem is still there. I'm using renderString(), not the async methods. Syntax errors do raise an exception, while import errors, macro errors (and maybe others) are still silently swallowed. This makes debugging complex templates very hard.

@leosdad two questions: are you passing a callback into renderString(), and what version of nunjucks do you have installed?

If the answer to the first question is no, and you're using the latest version, would it be possible to paste a minimal test case showing the problem?

The example provided in the comment above is fixed in the latest version. So if you're experiencing an issue with exceptions being swallowed, it is likely a different problem and ought to be its own issue.

Hi, @fdintino! Yes, I'm using the latest version, and I'm using no callback with renderString(). I'll try to reduce the problem code to the minimum and post it here ASAP. Thanks!

Thank you. And just to make sure I don't miss it, could you instead post it in a new issue?

Sure, I'll do that.

I've opened issue #1272 with the test case.

Was this page helpful?
0 / 5 - 0 ratings