Skip to content
Myr

Improve error messages when non-function provided as middleware

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

closes #3426

authored by Hrvoje Šimić · committed by Douglas Christopher Wilson · parent 12c3712 · Browse files · 6 files+49 added−23 removed
This commit

State

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

Diff stats

6 files+49 added−23 removed

  • tests:+43 added−18 removed
  • other:+6 added−5 removed

6 files+49 added−23 removed

modified:lib/router/index.js+2 added-2 removed
446 unmodified lines
447448 var callbacks = flatten(slice.call(arguments, offset));449450 if (callbacks.length === 0) {451 throw new TypeError('Router.use() requires middleware functions');451 throw new TypeError('Router.use() requires a middleware function')452 }453454 for (var i = 0; i < callbacks.length; i++) {455 var fn = callbacks[i];456457 if (typeof fn !== 'function') {458 throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))459 }460461 // add the middleware462 debug('use %o %s', path, fn.name || '<anonymous>')
200 unmodified lines
modified:lib/router/route.js+2 added-2 removed
173 unmodified lines
174 var handle = handles[i];175176 if (typeof handle !== 'function') {177 var type = toString.call(handle);178 var msg = 'Route.all() requires callback functions but got a ' + type;178 var msg = 'Route.all() requires a callback function but got a ' + type179 throw new TypeError(msg);180 }181182 var layer = Layer('/', {}, handle);
14 unmodified lines
197 var handle = handles[i];198199 if (typeof handle !== 'function') {200 var type = toString.call(handle);201 var msg = 'Route.' + method + '() requires callback functions but got a ' + type;201 var msg = 'Route.' + method + '() requires a callback function but got a ' + type202 throw new Error(msg);203 }204205 debug('%s %o', method, this.path)
11 unmodified lines
modified:lib/application.js+1 added-1 removed
205 unmodified lines
206207 var fns = flatten(slice.call(arguments, offset));208209 if (fns.length === 0) {210 throw new TypeError('app.use() requires middleware functions');210 throw new TypeError('app.use() requires a middleware function')211 }212213 // setup router214 this.lazyrouter();
430 unmodified lines
modified:test/app.use.js+22 added-9 removed
12var after = require('after');3var assert = require('assert')4var express = require('..');5var request = require('supertest');67describe('app', function(){
245 unmodified lines
253 })254 })255256 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/);257 it('should require middleware', function () {258 var app = express()259 assert.throws(function () { app.use('/') }, /requires a middleware function/)260 })261261 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/);262 it('should reject string as middleware', function () {263 var app = express()264 assert.throws(function () { app.use('/', 'foo') }, /requires a middleware function but got a string/)265 })266267 it('should reject number as middleware', function () {268 var app = express()269 assert.throws(function () { app.use('/', 42) }, /requires a middleware function but got a number/)270 })271272 it('should reject null as middleware', function () {273 var app = express()274 assert.throws(function () { app.use('/', null) }, /requires a middleware function but got a Null/)275 })276277 it('should reject Date as middleware', function () {278 var app = express()279 assert.throws(function () { app.use('/', new Date()) }, /requires a middleware function but got a Date/)280 })281282 it('should strip path from req.url', function (done) {283 var app = express();284285 app.use('/foo', function (req, res) {
255 unmodified lines
modified:test/Router.js+21 added-9 removed
366 unmodified lines
367 })368 })369370 describe('.use', function() {371 it('should require arguments', function(){372 var router = new Router();373 router.use.bind(router).should.throw(/requires middleware function/)371 it('should require middleware', function () {372 var router = new Router()373 assert.throws(function () { router.use('/') }, /requires a middleware function/)374 })375376 it('should not accept non-functions', function(){377 var router = new Router();378 router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)379 router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)380 router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)381 router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)376 it('should reject string as middleware', function () {377 var router = new Router()378 assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)379 })380381 it('should reject number as middleware', function () {382 var router = new Router()383 assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)384 })385386 it('should reject null as middleware', function () {387 var router = new Router()388 assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)389 })390391 it('should reject Date as middleware', function () {392 var router = new Router()393 assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)394 })395396 it('should be called for any URL', function (done) {397 var cb = after(4, done)398 var router = new Router()399
164 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Improve error message when autoloading invalid view engine5 * Improve error messages when non-function provided as middleware6 * Skip `Buffer` encoding when not generating ETag for small response7 * Use `safe-buffer` for improved Buffer API8 * deps: accepts@~1.3.49 - deps: mime-types@~2.1.16
3332 unmodified lines