Skip to content

Commit

Permalink
catch template syntax errors
Browse files Browse the repository at this point in the history
Rendering a template with syntax errors throws. Catch this and call the
callback to pass the error along to the error handling middleware.

Add tests to check for proper handling of the error.

fixes pillarjs#35
  • Loading branch information
defunctzombie committed Oct 25, 2012
1 parent 92c0195 commit 4238b86
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ exports.__express = function(filename, options, cb) {
cache[filename] = template;
}

var res = template(locals);
async.done(function(values) {
Object.keys(values).forEach(function(id) {
res = res.replace(id, values[id]);
try {
var res = template(locals);
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
21 changes: 20 additions & 1 deletion test/2.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ app.get('/', function(req, res){
});
});

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

test('index', function(done) {
app.listen(3000, function() {

Expand All @@ -85,7 +89,22 @@ test('index', function(done) {
});
});

app.on('close', function() {
app.once('close', function() {
done();
});
});

test('syntax error', function(done) {
app.listen(3000, function() {

request('http://localhost:3000/syntax-error', function(err, res, body) {
assert.equal(res.statusCode, 500);
assert.equal(body.split('\n')[0], 'Error: Parse error on line 1:');
app.close();
});
});

app.once('close', function() {
done();
});
});
1 change: 1 addition & 0 deletions test/2.x/views/syntax-error.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{foo}
20 changes: 20 additions & 0 deletions test/3.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ app.get('/html', function(req, res) {
});
});

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

test('index', function(done) {
var server = app.listen(3000, function() {

Expand Down Expand Up @@ -142,3 +146,19 @@ test('html extension', function(done) {
done();
});
});

test('syntax error', function(done) {
var server = app.listen(3000, function() {

request('http://localhost:3000/syntax-error', function(err, res, body) {
assert.equal(res.statusCode, 500);
assert.equal(body.split('\n')[0], 'Error: ' + __dirname + '/views/syntax-error.hbs: Parse error on line 1:');
//assert.equal(bod);
server.close();
});
});

server.on('close', function() {
done();
});
});
1 change: 1 addition & 0 deletions test/3.x/views/syntax-error.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{foo}

0 comments on commit 4238b86

Please sign in to comment.