Skip to content

Commit

Permalink
Fix handling of exceptions from layout
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 27, 2019
1 parent 39434ad commit 19b90c4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 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 from layout
* Fix handling of exceptions when cache enabled
* deps: [email protected]
* deps: [email protected]
Expand Down
41 changes: 18 additions & 23 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,27 @@ function middleware(filename, options, cb) {
}

// render with a layout
function render_with_layout(template, locals, cb) {
function render_with_layout (filename, template, locals, cb) {
render_file(locals, function(err, str) {
if (err) {
return cb(err);
}

locals.body = str;

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)
}
});
}

Expand Down Expand Up @@ -136,22 +141,12 @@ function middleware(filename, options, cb) {
return view_path;
});

var layout_template = layout_filename.reduce(function (cached, filename) {
if (cached) {
return cached;
}

var cached_file = cache[filename];
for (var i = 0; i < layout_filename.length; i++) {
var layout_template = cache[layout_filename[i]]

if (cached_file) {
return cache[filename];
if (layout_template) {
return render_with_layout(layout_filename[i], layout_template, options, cb)
}

return undefined;
}, undefined);

if (layout_template) {
return render_with_layout(layout_template, options, cb);
}

// TODO check if layout path has .hbs extension
Expand All @@ -162,7 +157,7 @@ function middleware(filename, options, cb) {
cache[filename] = layout_template;
}

render_with_layout(layout_template, options, cb);
render_with_layout(filename, layout_template, options, cb)
}

function tryReadFileAndCache(templates) {
Expand Down
12 changes: 12 additions & 0 deletions test/4.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ before(function () {
})
})

app.get('/syntax-error-layout', function (req, res) {
res.render('blank', { layout: 'bad_layout' })
})

app.get('/partials', function (req, res) {
res.render('partials', { layout: false })
})
Expand Down Expand Up @@ -225,6 +229,14 @@ test('syntax error cached', function (done) {
.end(done)
})

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

test('escape for frontend', function(done) {
request(app)
.get('/escape')
Expand Down
9 changes: 9 additions & 0 deletions test/4.x/views/bad_layout.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>{{title}</title>
<link rel='stylesheet' href='/stylesheets/style.css'>
</head>
<body>
{{{body}}}
</body>
</html>

0 comments on commit 19b90c4

Please sign in to comment.