Skip to content

Commit

Permalink
Fix issue where sync render would not raise errors in included templates
Browse files Browse the repository at this point in the history
fixes #1272
  • Loading branch information
fdintino committed Jul 18, 2020
1 parent 63c4baf commit bbcbaf3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Changelog
Merge of [#1276](https://github.com/mozilla/nunjucks/pull/1276); fixes
[#1198](https://github.com/mozilla/nunjucks/issues/1198). Thanks
[ogonkov](https://github.com/ogonkovv)!
* Fix bug that prevented errors in included templates from being raised when
rendering templates synchronously. Fixes
[#1272](https://github.com/mozilla/nunjucks/pull/1276).

3.2.1 (Mar 17 2020)
-------------------
Expand Down
13 changes: 7 additions & 6 deletions nunjucks/src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,15 @@ class Template extends Obj {
let didError = false;

this.rootRenderFunc(this.env, context, frame, globalRuntime, (err, res) => {
if (didError) {
// TODO: this is actually a bug in the compiled template (because waterfall
// tasks are both not passing errors up the chain of callbacks AND are not
// causing a return from the top-most render function). But fixing that
// will require a more substantial change to the compiler.
if (didError && cb && typeof res !== 'undefined') {
// prevent multiple calls to cb
if (cb) {
return;
} else {
throw err;
}
return;
}

if (err) {
err = lib._prettifyError(this.path, this.env.opts.dev, err);
didError = true;
Expand Down
19 changes: 19 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,25 @@
});
}

it('should throw exceptions from included templates when called synchronously', function() {
function templateRender() {
render('{% include "broken-import.njk" %}', {str: 'abc'});
}
expect(templateRender).to.throwException(/template not found: doesnotexist/);
});

it('should pass errors from included templates to callback when async', function(done) {
render(
'{% include "broken-import.njk" %}',
{str: 'abc'},
{noThrow: true},
function(err, res) {
expect(err).to.match(/template not found: doesnotexist/);
expect(res).to.be(undefined);
done();
});
});

it('should compile string concatenations with tilde', function(done) {
equal('{{ 4 ~ \'hello\' }}', '4hello');
equal('{{ 4 ~ 5 }}', '45');
Expand Down
2 changes: 2 additions & 0 deletions tests/templates/broken-import.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% import 'doesnotexist' as doesnotexist %}
str = {{ str | undefinedfilter }}

0 comments on commit bbcbaf3

Please sign in to comment.