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
Provenance
- Commit
2df1ad2- Tree
25ae455- Author
Hrvoje Šimić <shime@twobucks.co>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
12c3712
6 files changed+49 added−23 removed
modified:lib/router/index.js+2 added-2 removed
446 unmodified lines447 448 var callbacks = flatten(slice.call(arguments, offset));449 450 if (callbacks.length === 0) {451- throw new TypeError('Router.use() requires middleware functions');451+ throw new TypeError('Router.use() requires a middleware function')452 }453 454 for (var i = 0; i < callbacks.length; i++) {455 var fn = callbacks[i];456 457 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 }460 461 // add the middleware462 debug('use %o %s', path, fn.name || '<anonymous>')200 unmodified linesmodified:lib/router/route.js+2 added-2 removed
173 unmodified lines174 var handle = handles[i];175 176 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 }181 182 var layer = Layer('/', {}, handle);14 unmodified lines197 var handle = handles[i];198 199 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 }204 205 debug('%s %o', method, this.path)11 unmodified linesmodified:lib/application.js+1 added-1 removed
205 unmodified lines206 207 var fns = flatten(slice.call(arguments, offset));208 209 if (fns.length === 0) {210- throw new TypeError('app.use() requires middleware functions');210+ throw new TypeError('app.use() requires a middleware function')211 }212 213 // setup router214 this.lazyrouter();430 unmodified linesmodified:test/app.use.js+22 added-9 removed
1 2 var after = require('after');3+var assert = require('assert')4 var express = require('..');5 var request = require('supertest');6 7 describe('app', function(){245 unmodified lines253 })254 })255 256 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 })261 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/);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 })266 267+ 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+ })271+272+ 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+ })276+277+ 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+ })281+282 it('should strip path from req.url', function (done) {283 var app = express();284 285 app.use('/foo', function (req, res) {255 unmodified linesmodified:test/Router.js+21 added-9 removed
366 unmodified lines367 })368 })369 370 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 })375 376- 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 })380 381+ 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+ })385+386+ 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+ })390+391+ 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+ })395+396 it('should be called for any URL', function (done) {397 var cb = after(4, done)398 var router = new Router()399 164 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4 * 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.163332 unmodified lines