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