Skip to content

Commit 19b90c4

Browse files
committed
Fix handling of exceptions from layout
1 parent 39434ad commit 19b90c4

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==========
33

44
* Fix async helpers not working when cache enabled
5+
* Fix handling of exceptions from layout
56
* Fix handling of exceptions when cache enabled
67
78

lib/hbs.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,27 @@ function middleware(filename, options, cb) {
9090
}
9191

9292
// render with a layout
93-
function render_with_layout(template, locals, cb) {
93+
function render_with_layout (filename, template, locals, cb) {
9494
render_file(locals, function(err, str) {
9595
if (err) {
9696
return cb(err);
9797
}
9898

9999
locals.body = str;
100100

101-
var res = template(locals, handlebarsOpts);
102-
self.async.done(function(values) {
103-
Object.keys(values).forEach(function(id) {
104-
res = res.replace(id, values[id]);
105-
});
101+
try {
102+
var res = template(locals, handlebarsOpts)
103+
self.async.done(function (values) {
104+
Object.keys(values).forEach(function (id) {
105+
res = res.replace(id, values[id])
106+
})
106107

107-
cb(null, res);
108-
});
108+
cb(null, res)
109+
})
110+
} catch (err) {
111+
err.message = filename + ': ' + err.message
112+
cb(err)
113+
}
109114
});
110115
}
111116

@@ -136,22 +141,12 @@ function middleware(filename, options, cb) {
136141
return view_path;
137142
});
138143

139-
var layout_template = layout_filename.reduce(function (cached, filename) {
140-
if (cached) {
141-
return cached;
142-
}
143-
144-
var cached_file = cache[filename];
144+
for (var i = 0; i < layout_filename.length; i++) {
145+
var layout_template = cache[layout_filename[i]]
145146

146-
if (cached_file) {
147-
return cache[filename];
147+
if (layout_template) {
148+
return render_with_layout(layout_filename[i], layout_template, options, cb)
148149
}
149-
150-
return undefined;
151-
}, undefined);
152-
153-
if (layout_template) {
154-
return render_with_layout(layout_template, options, cb);
155150
}
156151

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

165-
render_with_layout(layout_template, options, cb);
160+
render_with_layout(filename, layout_template, options, cb)
166161
}
167162

168163
function tryReadFileAndCache(templates) {

test/4.x/app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ before(function () {
144144
})
145145
})
146146

147+
app.get('/syntax-error-layout', function (req, res) {
148+
res.render('blank', { layout: 'bad_layout' })
149+
})
150+
147151
app.get('/partials', function (req, res) {
148152
res.render('partials', { layout: false })
149153
})
@@ -225,6 +229,14 @@ test('syntax error cached', function (done) {
225229
.end(done)
226230
})
227231

232+
test('syntax error layout', function (done) {
233+
request(app)
234+
.get('/syntax-error-layout')
235+
.expect(500)
236+
.expect(shouldHaveFirstLineEqual('Error: ' + path.join(__dirname, 'views', 'bad_layout.hbs') + ': Parse error on line 3:'))
237+
.end(done)
238+
})
239+
228240
test('escape for frontend', function(done) {
229241
request(app)
230242
.get('/escape')

test/4.x/views/bad_layout.hbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>{{title}</title>
4+
<link rel='stylesheet' href='/stylesheets/style.css'>
5+
</head>
6+
<body>
7+
{{{body}}}
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)