Skip to content
Myr

fixed route error handlers when errors are thrown

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

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent f977272 · Browse files · 2 files+53 added−13 removed
This commit

State

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

Diff stats

2 files+53 added−13 removed

  • tests:+36 added−0 removed
  • other:+17 added−13 removed

2 files+53 added−13 removed

modified:lib/router/index.js+17 added-13 removed
272 unmodified lines
273 };274275 // invoke middleware callback276 function done() {277 var fn = route.callback278 , i = req._route_index + 1;277 var fn = route.callback;278279 try {280 if (error) {281 if (fn.length < 4) return pass(i);282 fn(error, req, res, function(err){283 error = err;284 pass(i);285 });286 } else {287 fn(req, res, function(err){288 error = err;289 pass(i);290 });280 if (error) {281 if (fn.length < 4) return nextRoute();282 fn(error, req, res, function(err){283 error = err;284 nextRoute();285 });286 } else {287 fn(req, res, function(err){288 error = err;289 nextRoute();290 });291 }292 } catch (err) {293 error = err;294 nextRoute();295 }296 }297 })(0);298};
128 unmodified lines
modified:test/router.test.js+36 added
745 unmodified lines
746 calls.should.eql(['one', 'two']);747 });748 },749 750 'test route callback thrown error handling': function(){751 var app = express.createServer()752 , calls = [];753754 app.get('/user/:id', function(req, res, next){755 calls.push('one');756 next();757 });758759 app.get('/user/:id', function(req, res, next){760 calls.push('two');761 throw new Error('fail');762 });763764 app.get('/user/:id', function(req, res, next){765 calls.push('three');766 next();767 });768 769 app.get('/user/*', function(err, req, res, next){770 res.statusCode = 500;771 res.send('error: ' + err.message);772 });773774 app.get('/user/*', function(req, res, next){775 calls.push('four');776 next();777 });778779 assert.response(app,780 { url: '/user/12' },781 { body: 'error: fail' }, function(){782 calls.should.eql(['one', 'two']);783 });784 },785 786 'test route callback error recovery': function(){787 var app = express.createServer();788789 app.get('/user/:id', function(req, res, next){
17 unmodified lines