Skip to content

Commit 2df1ad2

Browse files
shimedougwilson
authored andcommitted
Improve error messages when non-function provided as middleware
closes #3426
1 parent 12c3712 commit 2df1ad2

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==========
33

44
* Improve error message when autoloading invalid view engine
5+
* Improve error messages when non-function provided as middleware
56
* Skip `Buffer` encoding when not generating ETag for small response
67
* Use `safe-buffer` for improved Buffer API
78
* deps: accepts@~1.3.4

lib/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ app.use = function use(fn) {
207207
var fns = flatten(slice.call(arguments, offset));
208208

209209
if (fns.length === 0) {
210-
throw new TypeError('app.use() requires middleware functions');
210+
throw new TypeError('app.use() requires a middleware function')
211211
}
212212

213213
// setup router

lib/router/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,14 @@ proto.use = function use(fn) {
448448
var callbacks = flatten(slice.call(arguments, offset));
449449

450450
if (callbacks.length === 0) {
451-
throw new TypeError('Router.use() requires middleware functions');
451+
throw new TypeError('Router.use() requires a middleware function')
452452
}
453453

454454
for (var i = 0; i < callbacks.length; i++) {
455455
var fn = callbacks[i];
456456

457457
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))
459459
}
460460

461461
// add the middleware

lib/router/route.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Route.prototype.all = function all() {
175175

176176
if (typeof handle !== 'function') {
177177
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 ' + type
179179
throw new TypeError(msg);
180180
}
181181

@@ -198,7 +198,7 @@ methods.forEach(function(method){
198198

199199
if (typeof handle !== 'function') {
200200
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 ' + type
202202
throw new Error(msg);
203203
}
204204

test/Router.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,29 @@ describe('Router', function(){
368368
})
369369

370370
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/)
374374
})
375375

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/)
382394
})
383395

384396
it('should be called for any URL', function (done) {

test/app.use.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
var after = require('after');
3+
var assert = require('assert')
34
var express = require('..');
45
var request = require('supertest');
56

@@ -253,17 +254,29 @@ describe('app', function(){
253254
})
254255

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/)
259260
})
260261

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/)
267280
})
268281

269282
it('should strip path from req.url', function (done) {

0 commit comments

Comments
 (0)