From 606d0bc57ef0c97998239161e38c4e0b5f37d259 Mon Sep 17 00:00:00 2001 From: Frankie Dintino Date: Fri, 17 Jul 2020 21:48:10 -0400 Subject: [PATCH] Fix issue where sync render would not raise errors in included templates fixes #1272 --- CHANGELOG.md | 3 +++ nunjucks/src/environment.js | 13 +++++++------ tests/compiler.js | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b17d2bc5..2956b9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------- diff --git a/nunjucks/src/environment.js b/nunjucks/src/environment.js index 1cd75371..14169746 100644 --- a/nunjucks/src/environment.js +++ b/nunjucks/src/environment.js @@ -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; diff --git a/tests/compiler.js b/tests/compiler.js index c1c38ec3..8e133e7f 100644 --- a/tests/compiler.js +++ b/tests/compiler.js @@ -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');