From 22d887a67d24ff7beba5e0fbd07fa50cbabecc4d Mon Sep 17 00:00:00 2001 From: Ryan Bohn Date: Tue, 8 Oct 2013 21:48:46 -0400 Subject: [PATCH 1/2] When a callback is passed into compileFile, catch all errors thrown by compile and pass the error to callback --- lib/swig.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/swig.js b/lib/swig.js index 1565fac2..785e32e3 100644 --- a/lib/swig.js +++ b/lib/swig.js @@ -636,7 +636,17 @@ exports.Swig = function (opts) { cb(err); return; } - cb(err, self.compile(src, options)); + + var compiled; + + try { + compiled = self.compile(src, options); + } catch (err2) { + cb(err2); + return; + } + + cb(err, compiled); }); return; } From dc8e49dc92f4b627052917cc5c5094171097c088 Mon Sep 17 00:00:00 2001 From: Ryan Bohn Date: Tue, 8 Oct 2013 22:17:04 -0400 Subject: [PATCH 2/2] adding test case and linking --- lib/swig.js | 1 - tests/basic.test.js | 8 ++++++++ tests/cases-error/extends-non-existent.test.html | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/cases-error/extends-non-existent.test.html diff --git a/lib/swig.js b/lib/swig.js index 785e32e3..a182544f 100644 --- a/lib/swig.js +++ b/lib/swig.js @@ -636,7 +636,6 @@ exports.Swig = function (opts) { cb(err); return; } - var compiled; try { diff --git a/tests/basic.test.js b/tests/basic.test.js index 1e3ccc27..37e72fb6 100644 --- a/tests/basic.test.js +++ b/tests/basic.test.js @@ -187,6 +187,14 @@ describe('swig.compileFile', function () { })); }); }); + + it('can use callback with errors', function (done) { + var errorTest = __dirname + '/cases-error/extends-non-existent.test.html'; + expect(swig.compileFile(errorTest, {}, function (err) { + expect(err.errno).to.equal(34); + done(); + })); + }); }); describe('swig.renderFile', function () { diff --git a/tests/cases-error/extends-non-existent.test.html b/tests/cases-error/extends-non-existent.test.html new file mode 100644 index 00000000..b841e7ac --- /dev/null +++ b/tests/cases-error/extends-non-existent.test.html @@ -0,0 +1 @@ +{% extends 'does-not-exist.html' %} \ No newline at end of file