Improve error message for bad app.use arguments
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 3c1a964 · Browse files · 4 files·+42 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+42 added−12 removed
- tests:+18 added−7 removed
- other:+24 added−5 removed
Provenance
- Commit
bf1980f- Tree
6dbbdec- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
3c1a964
4 files changed+42 added−12 removed
modified:lib/router/index.js+23 added-5 removed
1+2 /**3 * Module dependencies.4 */5 2 unmodified lines8 var methods = require('methods');9 var mixin = require('utils-merge');10 var debug = require('debug')('express:router');11 var parseUrl = require('parseurl');12+var utils = require('../utils');13+14+/**15+ * Module variables.16+ */17+18+var objectRegExp = /^\[object (\S+)\]$/;19 var slice = Array.prototype.slice;20 var toString = Object.prototype.toString;13-var utils = require('../utils');21 22 /**23 * Initialize a new `Router` with the given `options`.24 *394 unmodified lines419 420 var callbacks = utils.flatten(slice.call(arguments, offset));421 422 if (callbacks.length === 0) {416- throw new TypeError('Router.use() requires callback function');423+ throw new TypeError('Router.use() requires middleware functions');424 }425 426 callbacks.forEach(function (fn) {427 if (typeof fn !== 'function') {421- var type = toString.call(fn);422- var msg = 'Router.use() requires callback function but got a ' + type;423- throw new TypeError(msg);428+ throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));429 }430 431 // add the middleware432 debug('use %s %s', path, fn.name || '<anonymous>');48 unmodified lines481 return this;482 };483 });484 485+// get type for error message486+function gettype(obj) {487+ var type = typeof obj;488+489+ if (type !== 'object') {490+ return type;491+ }492+493+ // inspect [[Class]] for objects494+ return toString.call(obj)495+ .replace(objectRegExp, '$1');496+}497+498 // merge params with parent params499 function mergeParams(params, parent) {500 if (typeof parent !== 'object' || !parent) {501 return params;65 unmodified linesmodified:test/app.use.js+13 added-5 removed
14 unmodified lines15 16 app.use(blog);17 })18 19- it('should reject missing functions', function(){20- var app = express();21- app.use.bind(app, 3).should.throw(/requires middleware function/);22- })23-19 describe('.use(app)', function(){20 it('should mount the app', function(done){21 var blog = express()22 , app = express();229 unmodified lines252 })253 })254 255 describe('.use(path, middleware)', function(){256+ it('should reject missing functions', function () {257+ var app = express();258+ app.use.bind(app, '/').should.throw(/requires middleware function/);259+ })260+261+ it('should reject non-functions as middleware', function () {262+ var app = express();263+ app.use.bind(app, '/', 'hi').should.throw(/requires middleware function.*string/);264+ app.use.bind(app, '/', 5).should.throw(/requires middleware function.*number/);265+ app.use.bind(app, '/', null).should.throw(/requires middleware function.*Null/);266+ app.use.bind(app, '/', new Date()).should.throw(/requires middleware function.*Date/);267+ })268+269 it('should strip path from req.url', function (done) {270 var app = express();271 272 app.use('/foo', function (req, res) {255 unmodified linesmodified:test/Router.js+5 added-2 removed
298 unmodified lines299 300 describe('.use', function() {301 it('should require arguments', function(){302 var router = new Router();303- router.use.bind(router).should.throw(/requires callback function/)303+ router.use.bind(router).should.throw(/requires middleware function/)304 })305 306 it('should not accept non-functions', function(){307 var router = new Router();308- router.use.bind(router, '/', 'hello').should.throw(/requires callback function/)308+ router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)309+ router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)310+ router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)311+ router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)312 })313 })314 315 describe('.param', function() {126 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4 * Fix regression for empty string `path` in `app.use`5+ * Improve error message for bad `app.use` arguments6 7 4.9.1 / 2014-09-168 ==================9 2250 unmodified lines