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 85918ef commit 606d0bc
Show file tree
Hide file tree
Showing 3 changed files with 29 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 it will be
// easier to rewrite the compiler than to fix that problem.
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

0 comments on commit 606d0bc

Please sign in to comment.