diff --git a/lib/hbs.js b/lib/hbs.js index d0cf27b..565ec81 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -53,6 +53,19 @@ function middleware(filename, options, cb) { return res } + // Waiter.resolve() may invoke this on a later tick, outside the try/catch + // around template(). Turn substitution throws into a render error so they + // do not become uncaught exceptions. + function finishAsync (filename, res, values, cb) { + var out + try { + out = replaceAsyncValues(res, values) + } catch (err) { + return cb(prependFilenameToError(filename, err)) + } + cb(null, out) + } + // grab extension from filename // if we need a layout, we will look for one matching out extension var extension = path.extname(filename); @@ -77,7 +90,7 @@ function middleware(filename, options, cb) { try { var res = template(locals, handlebarsOpts) self.async.done(function (values) { - cb(null, replaceAsyncValues(res, values)) + finishAsync(filename, res, values, cb) }) } catch (err) { cb(prependFilenameToError(filename, err)) @@ -99,7 +112,7 @@ function middleware(filename, options, cb) { try { var res = template(locals, handlebarsOpts); self.async.done(function(values) { - cb(null, replaceAsyncValues(res, values)); + finishAsync(filename, res, values, cb); }); } catch (err) { cb(prependFilenameToError(filename, err)) @@ -119,7 +132,7 @@ function middleware(filename, options, cb) { try { var res = template(locals, handlebarsOpts) self.async.done(function (values) { - cb(null, replaceAsyncValues(res, values)) + finishAsync(filename, res, values, cb) }) } catch (err) { cb(prependFilenameToError(filename, err)) diff --git a/test/4.x/async_helper_escape.js b/test/4.x/async_helper_escape.js index 03d340b..22adff1 100644 --- a/test/4.x/async_helper_escape.js +++ b/test/4.x/async_helper_escape.js @@ -97,6 +97,63 @@ test('async helper non-string values keep their pre-escaping text output', funct }) }) +test('async helper with non-callable toHTML is a render error, not a process crash (uncached)', function (done) { + var hbs = require('../../').create() + var payload = JSON.parse('{"toHTML":"x"}') + hbs.registerAsyncHelper('bad', function (_opts, cb) { + process.nextTick(function () { + cb(payload) + }) + }) + render(hbs, makeView('async-bad-tohtml.hbs', '{{bad}}'), function (err) { + assert.ok(err, 'expected a render error instead of an uncaught throw') + assert.ok(/is not a function/.test(err.message), err.message) + done() + }) +}) + +test('async helper with non-callable toHTML is a render error, not a process crash (cached)', function (done) { + var hbs = require('../../').create() + var payload = JSON.parse('{"toHTML":"x"}') + hbs.registerAsyncHelper('bad', function (_opts, cb) { + process.nextTick(function () { + cb(payload) + }) + }) + var view = makeView('async-bad-tohtml-cached.hbs', '{{bad}}') + render(hbs, view, { cache: true }, function (err) { + assert.ok(err, 'expected a render error on the uncached path') + assert.ok(hbs.cache[view], 'expected first render to cache the template') + render(hbs, view, { cache: true }, function (err2) { + assert.ok(err2, 'expected a render error on the cached path') + assert.ok(/is not a function/.test(err2.message), err2.message) + done() + }) + }) +}) + +test('async helper with non-callable toHTML is a render error, not a process crash (layout)', function (done) { + var hbs = require('../../').create() + var payload = JSON.parse('{"toHTML":"x"}') + hbs.registerAsyncHelper('bad', function (_opts, cb) { + process.nextTick(function () { + cb(payload) + }) + }) + var layout = makeView('layout-bad-tohtml.hbs', '{{{body}}}{{bad}}') + var view = makeView('async-bad-tohtml-layout.hbs', 'ok') + hbs.__express(view, { + settings: { + views: tmpDir, + 'view options': { layout: path.basename(layout, '.hbs') } + } + }, function (err) { + assert.ok(err, 'expected a render error on the layout path') + assert.ok(/is not a function/.test(err.message), err.message) + done() + }) +}) + test('async helper output is HTML-escaped by {{...}} (layout path)', function (done) { var hbs = require('../../').create() hbs.registerAsyncHelper('userBio', function (_opts, cb) { cb(PAYLOAD) })