Skip to content
Myr

Added support for routes to handle errors. Closes #809

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

currently only the route end-point callbacks support this, however this will change in the near future to support route middleware etc

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d10b7b4 · Browse files · 5 files+75 added−35 removed
This commit

State

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

Diff stats

5 files+75 added−35 removed

  • tests:+57 added−27 removed
  • other:+18 added−8 removed

5 files+75 added−35 removed

modified:lib/router/index.js+18 added-6 removed
190 unmodified lines
191 */192193Router.prototype._dispatch = function(req, res, next){194 var params = this.params195 , self = this;195 , self = this196 , error;197198 // route dispatch199 (function pass(i){200 var paramCallbacks
15 unmodified lines
216 // implied OPTIONS217 if (!route && 'OPTIONS' == req.method) return self._options(req, res);218219 // no route219 if (!route) return next();220 if (!route) return next(error);221222 // we have a route223 // start at param 0224 req.params = route.params;
48 unmodified lines
273 };274275 // invoke middleware callback276 function done() {276 route.callback(req, res, function(err){277 if (err) return next(err);278 nextRoute();279 });277 var fn = route.callback278 , i = req._route_index + 1;279280 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 });291 }292 }293 })(0);294};295
127 unmodified lines
modified:lib/view.js-2 removed
177 unmodified lines
178 if ('string' == typeof name) {179 options[name] = object;180 } else if (name === global) {181 merge(options, object);182 } else {183 options.scope = object;182 }183 }184 return res.render(view, options, null, parent, true);185 }
249 unmodified lines
deleted:test/fixtures/person.jade-1 removed
1p #{label} #{this.name}
modified:test/router.test.js+57 added
707 unmodified lines
708 { url: '/foo' },709 function(){710 routes.should.eql(['/:foo?', '/foo']);711 });712 },713 714 'test route callback error handling': function(){715 var app = express.createServer()716 , calls = [];717718 app.get('/user/:id', function(req, res, next){719 calls.push('one');720 next();721 });722723 app.get('/user/:id', function(req, res, next){724 calls.push('two');725 next(new Error('fail'));726 });727728 app.get('/user/:id', function(req, res, next){729 calls.push('three');730 next();731 });732 733 app.get('/user/*', function(err, req, res, next){734 res.statusCode = 500;735 res.send('error: ' + err.message);736 });737738 app.get('/user/*', function(req, res, next){739 calls.push('four');740 next();741 });742743 assert.response(app,744 { url: '/user/12' },745 { body: 'error: fail' }, function(){746 calls.should.eql(['one', 'two']);747 });748 },749 750 'test route callback error recovery': function(){751 var app = express.createServer();752753 app.get('/user/:id', function(req, res, next){754 next(new Error('fail'));755 });756757 app.get('/user/*', function(err, req, res, next){758 req.error = err;759 next();760 });761762 app.get('/user/*', function(req, res, next){763 res.send('recovered from error: ' + req.error.message);764 });765766 assert.response(app,767 { url: '/user/12' },768 { body: 'recovered from error: fail' });769 }770};
modified:test/view.test.js-26 removed
393 unmodified lines
394 assert.response(app,395 { url: '/user' },396 { body: '<p>tj</p>' });397398 // as: this collection option399 app.get('/person', function(req, res){400 res.partial('person.jade', {401 as: this,402 collection: [{ name: 'tj' }],403 locals: { label: 'name:' }404 });405 });406 407 assert.response(app,408 { url: '/person' },409 { body: '<p>name: tj</p>' });410 398 // as: global collection option399 app.get('/videos', function(req, res){400 res.partial('video.jade', {401 as: global,
39 unmodified lines
441 assert.response(app,442 { url: '/video-global' },443 { body: '<p>Tim Burton</p>' });444 458 app.get('/person-this', function(req, res){459 res.partial('person.jade', {460 object: { name: 'tj' },461 locals: { label: 'User:' },462 as: this463 }); 464 });465 466 // Non-collection as: this467 assert.response(app,468 { url: '/person-this' },469 { body: '<p>User: tj</p>' });470 445 // No options446 app.get('/nothing', function(req, res){447 res.partial('hello.ejs');448 });
608 unmodified lines