Skip to content
Browse files

Revert "add support for `next(status[, msg])`"

see #723

This reverts commit 87ffafd.
  • Loading branch information...
1 parent 62ebde1 commit 6db901f967036ccc3c892b4bcb5bcb59e0b0dca9 @tj tj committed
Showing with 27 additions and 19 deletions.
  1. +2 −2 lib/middleware/basicAuth.js
  2. +1 −1 lib/middleware/csrf.js
  3. +4 −3 lib/middleware/directory.js
  4. +2 −2 lib/middleware/json.js
  5. +1 −1 lib/middleware/limit.js
  6. +1 −10 lib/proto.js
  7. +16 −0 lib/utils.js
View
4 lib/middleware/basicAuth.js
@@ -69,13 +69,13 @@ module.exports = function basicAuth(callback, realm) {
var parts = authorization.split(' ');
- if (parts.length !== 2) return next(400);
+ if (parts.length !== 2) return next(utils.error(400));
var scheme = parts[0]
, credentials = new Buffer(parts[1], 'base64').toString()
, index = credentials.indexOf(':');
- if ('Basic' != scheme || index < 0) return next(400);
+ if ('Basic' != scheme || index < 0) return next(utils.error(400));
var user = credentials.slice(0, index)
, pass = credentials.slice(index + 1);
View
2 lib/middleware/csrf.js
@@ -51,7 +51,7 @@ module.exports = function csrf(options) {
var val = value(req);
// check
- if (val != token) return next(403);
+ if (val != token) return next(utils.error(403));
next();
}
View
7 lib/middleware/directory.js
@@ -16,6 +16,7 @@
var fs = require('fs')
, parse = require('url').parse
+ , utils = require('../utils')
, path = require('path')
, normalize = path.normalize
, extname = path.extname
@@ -66,10 +67,10 @@ exports = module.exports = function directory(root, options){
, showUp = path != root && path != root + '/';
// null byte(s), bad request
- if (~path.indexOf('\0')) return next(400);
+ if (~path.indexOf('\0')) return next(utils.error(400));
// malicious path, forbidden
- if (0 != path.indexOf(root)) return next(403);
+ if (0 != path.indexOf(root)) return next(utils.error(403));
// check if we have a directory
fs.stat(path, function(err, stat){
@@ -95,7 +96,7 @@ exports = module.exports = function directory(root, options){
}
// not acceptable
- next(406);
+ next(utils.error(406));
});
});
};
View
4 lib/middleware/json.js
@@ -68,10 +68,10 @@ exports = module.exports = function(options){
var first = buf.trim()[0];
if (0 == buf.length) {
- return next(400, 'invalid json, empty body');
+ return next(utils.error(400, 'invalid json, empty body'));
}
- if (strict && '{' != first && '[' != first) return next(400, 'invalid json');
+ if (strict && '{' != first && '[' != first) return next(utils.error(400, 'invalid json'));
try {
req.body = JSON.parse(buf, options.reviver);
next();
View
2 lib/middleware/limit.js
@@ -42,7 +42,7 @@ module.exports = function limit(bytes){
req._limit = true;
// limit by content-length
- if (len && len > bytes) return next(413);
+ if (len && len > bytes) return next(utils.error(413));
// limit
req.on('data', function(chunk){
View
11 lib/proto.js
@@ -106,7 +106,7 @@ app.handle = function(req, res, out) {
, slashAdded = false
, index = 0;
- function next(err, msg) {
+ function next(err) {
var layer, path, status, c;
if (slashAdded) {
@@ -118,15 +118,6 @@ app.handle = function(req, res, out) {
req.originalUrl = req.originalUrl || req.url;
removed = '';
- // next(status, msg) support
- if (typeof err === 'number') {
- var status = err;
- var name = http.STATUS_CODES[status];
- err = new Error(msg || name);
- err.name = name;
- err.status = status;
- }
-
// next callback
layer = stack[index++];
View
16 lib/utils.js
@@ -42,6 +42,22 @@ exports.mime = function(req) {
};
/**
+ * Generate an `Error` from the given status `code`
+ * and optional `msg`.
+ *
+ * @param {Number} code
+ * @param {String} msg
+ * @return {Error}
+ * @api private
+ */
+
+exports.error = function(code, msg){
+ var err = new Error(msg || http.STATUS_CODES[code]);
+ err.status = code;
+ return err;
+};
+
+/**
* Return md5 hash of the given string and optional encoding,
* defaulting to hex.
*

0 comments on commit 6db901f

Please sign in to comment.
Something went wrong with that request. Please try again.