Skip to content
Myr

fix handling of errors from param callbacks

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes #2149

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent dfefea5 · Browse files · 3 files+44 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+44 added−2 removed

  • tests:+25 added−0 removed
  • other:+19 added−2 removed

3 files+44 added−2 removed

modified:lib/router/index.js+14 added-2 removed
288 unmodified lines
289 var paramIndex = 0;290 var key;291 var paramVal;292 var paramCallbacks;293 var paramCalled;294295 // process params in order296 // param callbacks can be async297 function param(err) {
14 unmodified lines
312313 name = key.name;314 paramVal = req.params[name];315 paramCallbacks = params[name];316 paramCalled = called[name];317316 if (paramVal === undefined || !paramCallbacks || called[name] === paramVal) {318 if (paramVal === undefined || !paramCallbacks) {319 return param();320 }321320 called[name] = paramVal;322 // param previously called with same value or error occurred323 if (paramCalled && (paramCalled.err || paramCalled.val === paramVal)) {324 return param(paramCalled.err);325 }326327 called[name] = paramCalled = { val: paramVal };328329 try {330 return paramCallback();331 } catch (err) {332 return done(err);
1 unmodified line
334 }335336 // single param callbacks337 function paramCallback(err) {338 if (err && paramCalled) {339 // store error340 paramCalled.err = err;341 }342343 var fn = paramCallbacks[paramIndex++];344 if (err || !fn) return param(err);345 fn(req, res, paramCallback, paramVal, key.name);346 }
89 unmodified lines
modified:test/app.param.js+25 added
192 unmodified lines
193 request(app)194 .get('/user/123')195 .expect('name', done);196 })197198 it('should defer all the param routes', function(done){199 var app = express();200201 app.param('id', function(req, res, next, val){202 if (val === 'new') return next('route');203 return next();204 });205206 app.all('/user/:id', function(req, res){207 res.send('all.id');208 });209210 app.get('/user/:id', function(req, res){211 res.send('get.id');212 });213214 app.get('/user/new', function(req, res){215 res.send('get.new');216 });217218 request(app)219 .get('/user/new')220 .expect('get.new', done);221 })222 })223})
modified:History.md+5 added
1unreleased2==========34 * fix handling of errors from `router.param()` callbacks564.3.1 / 2014-05-237==================89 * revert "fix behavior of multiple `app.VERB` for the same path"
1475 unmodified lines