Fix handling when route.all is only route
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent b89a597 · Browse files · 3 files·+35 added−31 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+35 added−31 removed
- tests:+26 added−0 removed
- other:+9 added−31 removed
Provenance
- Commit
7f26cfc- Tree
b04918f- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
b89a597
3 files changed+35 added−31 removed
modified:lib/router/route.js+8 added-31 removed
20 unmodified lines2122232425252627282915 unmodified lines4546474849504950515253545556575856596058596061626364656667686162636471 unmodified lines13613713813914814915015115215315415514015614114214314415 unmodified lines16016116216318018118218318418518616416516616716820 unmodified lines
function Route(path) { debug('new %s', path); this.path = path; this.stack = undefined; this.stack = [];
// route handlers for various http methods this.methods = {};}15 unmodified lines * @api private */
Route.prototype.dispatch = function(req, res, done){ var self = this; var method = req.method.toLowerCase(); var stack = this.stack; if (stack.length === 0) { return done(); }
var method = req.method.toLowerCase(); if (method === 'head' && !this.methods['head']) { method = 'get'; }
req.route = self; req.route = this;
// single middleware route case if (typeof this.stack === 'function') { this.stack(req, res, done); return; }
var stack = self.stack; if (!stack) { return done(); }
var idx = 0; (function next_layer(err) { if (err && err === 'route') { return done();71 unmodified lines var msg = 'Route.all() requires callback functions but got a ' + type; throw new Error(msg); }
if (!self.stack) { self.stack = fn; } else if (typeof self.stack === 'function') { self.stack = [{ handle: self.stack }, { handle: fn }]; } else { self.stack.push({ handle: fn }); self.stack.push({ handle: fn }); } });
return self;};15 unmodified lines if (!self.methods[method]) { self.methods[method] = true; }
if (!self.stack) { self.stack = []; } else if (typeof self.stack === 'function') { self.stack = [{ handle: self.stack }]; }
self.stack.push({ method: method, handle: fn }); }); return self; };});modified:test/Route.js+26 added
1 2+var should = require('should');3 var express = require('../')4 , Route = express.Route5 , methods = require('methods')6 , assert = require('assert');177 unmodified lines184 assert.equal(err.message, 'oops');185 done();186 });187 188+ route.dispatch({ url: '/', method: 'GET' }, {});189+ });190+191+ it('should handle throw in .all', function(done) {192+ var route = new Route('');193+194+ route.all(function(req, res, next){195+ throw new Error('boom!');196+ });197+198+ route.dispatch({ url: '/', method: 'GET' }, {}, function(err){199+ should(err).be.ok;200+ err.message.should.equal('boom!');201+ done();202+ });203+ });204+205+ it('should handle single error handler', function(done) {206+ var route = new Route('');207+208+ route.all(function(err, req, res, next){209+ // this should not execute210+ true.should.be.false;211+ });212+213 route.dispatch({ url: '/', method: 'GET' }, {}, done);214 });215 })216 })modified:History.md+1 added
1 unmodified line2 ==========3 4 * deprecate things with `depd` module5 * fix behavior when handling request without routes6+ * fix handling when `route.all` is only route7 * restore `req.params` after invoking router8 * use `finalhandler` for final response handling9 * use `media-typer` to alter content-type charset10 1680 unmodified lines