Navigation Menu

Skip to content

Commit

Permalink
handle thrown errors inside Route
Browse files Browse the repository at this point in the history
close #2029
  • Loading branch information
defunctzombie committed Apr 8, 2014
1 parent 1ee5329 commit d72f279
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/router/route.js
Expand Up @@ -87,14 +87,23 @@ Route.prototype.dispatch = function(req, res, done){
return next_layer(err);
}

return layer.handle(err, req, res, next_layer);
try {
layer.handle(err, req, res, next_layer);
} catch (err) {
next_layer(err);
}
return;
}

if (arity > 3) {
return next_layer();
}

layer.handle(req, res, next_layer);
try {
layer.handle(req, res, next_layer);
} catch (err) {
next_layer(err);
}
})();
};

Expand Down
27 changes: 27 additions & 0 deletions test/Route.js
Expand Up @@ -140,5 +140,32 @@ describe('Route', function(){

route.dispatch({ method: 'get' }, {});
})

it('should handle throw', function(done) {
var route = new Route('');

var order = '';
route.all(function(req, res, next){
throw new Error('foobar');
});

route.all(function(req, res, next){
order += '0';
next();
});

route.all(function(err, req, res, next){
order += 'a';
next(err);
});

route.all(function(err, req, res, next){
assert.equal(err.message, 'foobar');
assert.equal(order, 'a');
done();
});

route.dispatch({ method: 'get' }, {});
});
})
})

0 comments on commit d72f279

Please sign in to comment.