Skip to content

Commit

Permalink
Fix handling of exceptions when cache enabled
Browse files Browse the repository at this point in the history
closes #156
fixes #165
  • Loading branch information
agarzola authored and dougwilson committed Sep 27, 2019
1 parent e00c762 commit 39434ad
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Fix async helpers not working when cache enabled
* Fix handling of exceptions when cache enabled
* deps: handlebars@4.1.2
* deps: walk@2.3.14

Expand Down
19 changes: 12 additions & 7 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ function middleware(filename, options, cb) {
// cached?
var template = cache[filename];
if (template) {
var res = template(locals, handlebarsOpts)
self.async.done(function (values) {
Object.keys(values).forEach(function (id) {
res = res.replace(id, values[id])
})
try {
var res = template(locals, handlebarsOpts)
self.async.done(function (values) {
Object.keys(values).forEach(function (id) {
res = res.replace(id, values[id])
})

cb(null, res)
})
cb(null, res)
})
} catch (err) {
err.message = filename + ': ' + err.message
cb(err)
}

return
}
Expand Down
12 changes: 11 additions & 1 deletion test/3.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ before(function () {
})

app.get('/syntax-error', function (req, res) {
res.render('syntax-error')
res.render('syntax-error', {
cache: true
})
})

app.get('/partials', function (req, res) {
Expand Down Expand Up @@ -205,6 +207,14 @@ test('syntax error', function(done) {
.end(done)
});

test('syntax error cached', function (done) {
request(app)
.get('/syntax-error')
.expect(500)
.expect(shouldHaveFirstLineEqual('Error: ' + path.join(__dirname, 'views', 'syntax-error.hbs') + ': Parse error on line 1:'))
.end(done)
})

test('escape for frontend', function(done) {
request(app)
.get('/escape')
Expand Down
12 changes: 11 additions & 1 deletion test/4.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ before(function () {
})

app.get('/syntax-error', function (req, res) {
res.render('syntax-error')
res.render('syntax-error', {
cache: true
})
})

app.get('/partials', function (req, res) {
Expand Down Expand Up @@ -215,6 +217,14 @@ test('syntax error', function(done) {
.end(done)
});

test('syntax error cached', function (done) {
request(app)
.get('/syntax-error')
.expect(500)
.expect(shouldHaveFirstLineEqual('Error: ' + path.join(__dirname, 'views', 'syntax-error.hbs') + ': Parse error on line 1:'))
.end(done)
})

test('escape for frontend', function(done) {
request(app)
.get('/escape')
Expand Down

0 comments on commit 39434ad

Please sign in to comment.