mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 00:19:48 +01:00
Improve error messages when non-function provided as middleware
closes #3426
This commit is contained in:
parent
12c3712468
commit
2df1ad26a5
|
|
@ -2,6 +2,7 @@ unreleased
|
|||
==========
|
||||
|
||||
* Improve error message when autoloading invalid view engine
|
||||
* Improve error messages when non-function provided as middleware
|
||||
* Skip `Buffer` encoding when not generating ETag for small response
|
||||
* Use `safe-buffer` for improved Buffer API
|
||||
* deps: accepts@~1.3.4
|
||||
|
|
|
|||
|
|
@ -207,7 +207,7 @@ app.use = function use(fn) {
|
|||
var fns = flatten(slice.call(arguments, offset));
|
||||
|
||||
if (fns.length === 0) {
|
||||
throw new TypeError('app.use() requires middleware functions');
|
||||
throw new TypeError('app.use() requires a middleware function')
|
||||
}
|
||||
|
||||
// setup router
|
||||
|
|
|
|||
|
|
@ -448,14 +448,14 @@ proto.use = function use(fn) {
|
|||
var callbacks = flatten(slice.call(arguments, offset));
|
||||
|
||||
if (callbacks.length === 0) {
|
||||
throw new TypeError('Router.use() requires middleware functions');
|
||||
throw new TypeError('Router.use() requires a middleware function')
|
||||
}
|
||||
|
||||
for (var i = 0; i < callbacks.length; i++) {
|
||||
var fn = callbacks[i];
|
||||
|
||||
if (typeof fn !== 'function') {
|
||||
throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
|
||||
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
|
||||
}
|
||||
|
||||
// add the middleware
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ Route.prototype.all = function all() {
|
|||
|
||||
if (typeof handle !== 'function') {
|
||||
var type = toString.call(handle);
|
||||
var msg = 'Route.all() requires callback functions but got a ' + type;
|
||||
var msg = 'Route.all() requires a callback function but got a ' + type
|
||||
throw new TypeError(msg);
|
||||
}
|
||||
|
||||
|
|
@ -198,7 +198,7 @@ methods.forEach(function(method){
|
|||
|
||||
if (typeof handle !== 'function') {
|
||||
var type = toString.call(handle);
|
||||
var msg = 'Route.' + method + '() requires callback functions but got a ' + type;
|
||||
var msg = 'Route.' + method + '() requires a callback function but got a ' + type
|
||||
throw new Error(msg);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -368,17 +368,29 @@ describe('Router', function(){
|
|||
})
|
||||
|
||||
describe('.use', function() {
|
||||
it('should require arguments', function(){
|
||||
var router = new Router();
|
||||
router.use.bind(router).should.throw(/requires middleware function/)
|
||||
it('should require middleware', function () {
|
||||
var router = new Router()
|
||||
assert.throws(function () { router.use('/') }, /requires a middleware function/)
|
||||
})
|
||||
|
||||
it('should not accept non-functions', function(){
|
||||
var router = new Router();
|
||||
router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)
|
||||
router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)
|
||||
router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)
|
||||
router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)
|
||||
it('should reject string as middleware', function () {
|
||||
var router = new Router()
|
||||
assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)
|
||||
})
|
||||
|
||||
it('should reject number as middleware', function () {
|
||||
var router = new Router()
|
||||
assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)
|
||||
})
|
||||
|
||||
it('should reject null as middleware', function () {
|
||||
var router = new Router()
|
||||
assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)
|
||||
})
|
||||
|
||||
it('should reject Date as middleware', function () {
|
||||
var router = new Router()
|
||||
assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)
|
||||
})
|
||||
|
||||
it('should be called for any URL', function (done) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
var after = require('after');
|
||||
var assert = require('assert')
|
||||
var express = require('..');
|
||||
var request = require('supertest');
|
||||
|
||||
|
|
@ -253,17 +254,29 @@ describe('app', function(){
|
|||
})
|
||||
|
||||
describe('.use(path, middleware)', function(){
|
||||
it('should reject missing functions', function () {
|
||||
var app = express();
|
||||
app.use.bind(app, '/').should.throw(/requires middleware function/);
|
||||
it('should require middleware', function () {
|
||||
var app = express()
|
||||
assert.throws(function () { app.use('/') }, /requires a middleware function/)
|
||||
})
|
||||
|
||||
it('should reject non-functions as middleware', function () {
|
||||
var app = express();
|
||||
app.use.bind(app, '/', 'hi').should.throw(/requires middleware function.*string/);
|
||||
app.use.bind(app, '/', 5).should.throw(/requires middleware function.*number/);
|
||||
app.use.bind(app, '/', null).should.throw(/requires middleware function.*Null/);
|
||||
app.use.bind(app, '/', new Date()).should.throw(/requires middleware function.*Date/);
|
||||
it('should reject string as middleware', function () {
|
||||
var app = express()
|
||||
assert.throws(function () { app.use('/', 'foo') }, /requires a middleware function but got a string/)
|
||||
})
|
||||
|
||||
it('should reject number as middleware', function () {
|
||||
var app = express()
|
||||
assert.throws(function () { app.use('/', 42) }, /requires a middleware function but got a number/)
|
||||
})
|
||||
|
||||
it('should reject null as middleware', function () {
|
||||
var app = express()
|
||||
assert.throws(function () { app.use('/', null) }, /requires a middleware function but got a Null/)
|
||||
})
|
||||
|
||||
it('should reject Date as middleware', function () {
|
||||
var app = express()
|
||||
assert.throws(function () { app.use('/', new Date()) }, /requires a middleware function but got a Date/)
|
||||
})
|
||||
|
||||
it('should strip path from req.url', function (done) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user