Nunjucks: Exceptions being swallowed after import errors in included templates

Created on 16 Mar 2020  路  7Comments  路  Source: mozilla/nunjucks

As stated in topic #1127, there are still problems with exceptions not being thrown in certain cases. I found out that if some problem happens with import errors inside included templates, Nunjucks will fail to generate the required exceptions. For example, create a template included.njk:

{% import 'unknown.njk' as nothing %}
String is {{ str | whatever }}

and the following script:

const path = 'C:/users/etc/thispath';
const nunjucks = require("nunjucks");
nunjucks.configure(path);
console.log(nunjucks.renderString("{% include 'included.njk' %}", { str: 'okay' }));

Nunjucks silently outputs a null string. Some notes:

  • The exception is captured correctly if the contents of the njk file are directly used as the template.
  • The exception is also captured if the line with import is removed from the njk file.

I hope this is clear. Thanks in advance!

Most helpful comment

Hi again,

Any prospects on this problem?

All 7 comments

(bump)

Any news? TIA

Hi again,

Any prospects on this problem?

This is quite the issue would be interested in some guidance.

Thanks for your attention. A minimal test case would be as follows.

  1. Create a file named _test.js_ with:
const nunjucks = require('nunjucks');
console.log(nunjucks.renderString('{% include "included.njk" %}', { str: 'okay' }));
  1. Create a file named _included.njk_:
String is {{ str }}
  1. Run the script test.js. You will get 'String is okay' as expected.

  2. Now, edit _included.njk_ so as to import a nonexistent file:

{% import 'unknown.njk' as dummy %}
String is {{ str }}
  1. Run test.js again. You will get 'null' as the result. I'd expect an exception instead, saying that _unknown.njk_ was not found or a similar error.

I hope it's clearer now!

Oh my bad! I'm experiencing the same error! Sorry for that confusion, was hoping someone could help me (us) figure that out.

I'm looking into this

Appreciate it! Will test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thisKai picture thisKai  路  3Comments

decibyte picture decibyte  路  6Comments

mattbasta picture mattbasta  路  6Comments

ricordisamoa picture ricordisamoa  路  5Comments

boutell picture boutell  路  6Comments