Skip to content
Myr

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

4 files+42 added−12 removed

modified:lib/router/index.js+23 added-5 removed
12/**3 * Module dependencies.4 */5
2 unmodified lines
8var methods = require('methods');9var mixin = require('utils-merge');10var debug = require('debug')('express:router');11var parseUrl = require('parseurl');12var utils = require('../utils');1314/**15 * Module variables.16 */1718var objectRegExp = /^\[object (\S+)\]$/;19var slice = Array.prototype.slice;20var toString = Object.prototype.toString;13var utils = require('../utils');2122/**23 * Initialize a new `Router` with the given `options`.24 *
394 unmodified lines
419420 var callbacks = utils.flatten(slice.call(arguments, offset));421422 if (callbacks.length === 0) {416 throw new TypeError('Router.use() requires callback function');423 throw new TypeError('Router.use() requires middleware functions');424 }425426 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 }430431 // add the middleware432 debug('use %s %s', path, fn.name || '<anonymous>');
48 unmodified lines
481 return this;482 };483});484485// get type for error message486function gettype(obj) {487 var type = typeof obj;488489 if (type !== 'object') {490 return type;491 }492493 // inspect [[Class]] for objects494 return toString.call(obj)495 .replace(objectRegExp, '$1');496}497498// merge params with parent params499function mergeParams(params, parent) {500 if (typeof parent !== 'object' || !parent) {501 return params;
65 unmodified lines
modified:test/app.use.js+13 added-5 removed
14 unmodified lines
1516 app.use(blog);17 })1819 it('should reject missing functions', function(){20 var app = express();21 app.use.bind(app, 3).should.throw(/requires middleware function/);22 })2319 describe('.use(app)', function(){20 it('should mount the app', function(done){21 var blog = express()22 , app = express();
229 unmodified lines
252 })253 })254255 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 })260261 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 })268269 it('should strip path from req.url', function (done) {270 var app = express();271272 app.use('/foo', function (req, res) {
255 unmodified lines
modified:test/Router.js+5 added-2 removed
298 unmodified lines
299300 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 })305306 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 })314315 describe('.param', function() {
126 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Fix regression for empty string `path` in `app.use`5 * Improve error message for bad `app.use` arguments674.9.1 / 2014-09-168==================9
2250 unmodified lines