Catch errors in multiple req.param(name, fn) handlers

This commit is contained in:
Douglas Christopher Wilson 2014-07-08 01:15:31 -04:00
parent 76e8bfa1dc
commit a01326adac
3 changed files with 28 additions and 6 deletions

View File

@ -3,6 +3,7 @@ unreleased
* add explicit "Rosetta Flash JSONP abuse" protection
- previous versions are not vulnerable; this is just explicit protection
* catch errors in multiple `req.param(name, fn)` handlers
* deprecate `res.redirect(url, status)` -- use `res.redirect(status, url)` instead
* fix `res.send(status, num)` to send `num` as json (not error)
* remove unnecessary escaping when `res.jsonp` returns JSON response

View File

@ -353,11 +353,7 @@ proto.process_params = function(layer, called, req, res, done) {
value: paramVal
};
try {
return paramCallback();
} catch (err) {
return done(err);
}
paramCallback();
}
// single param callbacks
@ -376,7 +372,11 @@ proto.process_params = function(layer, called, req, res, done) {
if (!fn) return param();
fn(req, res, paramCallback, paramVal, key.name);
try {
fn(req, res, paramCallback, paramVal, key.name);
} catch (e) {
paramCallback(e);
}
}
param();

View File

@ -237,6 +237,27 @@ describe('app', function(){
.expect(500, done);
})
it('should catch thrown secondary error', function(done){
var app = express();
app.param('id', function(req, res, next, val){
process.nextTick(next);
});
app.param('id', function(req, res, next, id){
throw new Error('err!');
});
app.get('/user/:id', function(req, res){
var id = req.params.id;
res.send('' + id);
});
request(app)
.get('/user/123')
.expect(500, done);
})
it('should defer to next route', function(done){
var app = express();