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

Errors seem to be swallowed. #1127

Closed
davidjamesstone opened this issue Jul 13, 2018 · 12 comments · Fixed by #1181
Closed

Errors seem to be swallowed. #1127

davidjamesstone opened this issue Jul 13, 2018 · 12 comments · Fixed by #1181
Labels

Comments

@davidjamesstone
Copy link

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

@devoidfury
Copy link
Contributor

Can you provide the example illustrating the issue? Thanks!

@davidjamesstone
Copy link
Author

davidjamesstone commented Jul 25, 2018

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

@cgatesman
Copy link

cgatesman commented Aug 31, 2018

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.

@fdintino fdintino added the bug label Sep 1, 2018
@TomFrost
Copy link

TomFrost commented Sep 4, 2018

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.

@TomFrost
Copy link

TomFrost commented Sep 4, 2018

Looks like this could be related to #678

@leosdad
Copy link

leosdad commented Mar 16, 2020

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.

@fdintino
Copy link
Collaborator

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

@fdintino
Copy link
Collaborator

fdintino commented Mar 16, 2020

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.

@leosdad
Copy link

leosdad commented Mar 16, 2020

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!

@fdintino
Copy link
Collaborator

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

@leosdad
Copy link

leosdad commented Mar 16, 2020

Sure, I'll do that.

@leosdad
Copy link

leosdad commented Mar 17, 2020

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

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

Successfully merging a pull request may close this issue.

6 participants