http-errors
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -0,1 +1,11 @@ | ||
2016-05-18 / 1.5.0 | ||
================== | ||
* Support new code `421 Misdirected Request` | ||
* Use `setprototypeof` module to replace `__proto__` setting | ||
* deps: statuses@'>= 1.3.0 < 2' | ||
- Add `421 Misdirected Request` | ||
- perf: enable strict mode | ||
* perf: enable strict mode | ||
2016-01-28 / 1.4.0 | ||
@@ -2,0 +12,0 @@ ================== |
241
index.js
@@ -0,34 +1,60 @@ | ||
/*! | ||
* http-errors | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2016 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
var statuses = require('statuses'); | ||
var inherits = require('inherits'); | ||
'use strict' | ||
function toIdentifier(str) { | ||
return str.split(' ').map(function (token) { | ||
return token.slice(0, 1).toUpperCase() + token.slice(1) | ||
}).join('').replace(/[^ _0-9a-z]/gi, '') | ||
} | ||
/** | ||
* Module dependencies. | ||
* @private | ||
*/ | ||
exports = module.exports = function httpError() { | ||
var setPrototypeOf = require('setprototypeof') | ||
var statuses = require('statuses') | ||
var inherits = require('inherits') | ||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
module.exports = createError | ||
module.exports.HttpError = createHttpErrorConstructor() | ||
// Populate exports for all constructors | ||
populateConstructorExports(module.exports, statuses.codes, module.exports.HttpError) | ||
/** | ||
* Create a new HTTP Error. | ||
* | ||
* @returns {Error} | ||
* @public | ||
*/ | ||
function createError () { | ||
// so much arity going on ~_~ | ||
var err; | ||
var msg; | ||
var status = 500; | ||
var props = {}; | ||
var err | ||
var msg | ||
var status = 500 | ||
var props = {} | ||
for (var i = 0; i < arguments.length; i++) { | ||
var arg = arguments[i]; | ||
var arg = arguments[i] | ||
if (arg instanceof Error) { | ||
err = arg; | ||
status = err.status || err.statusCode || status; | ||
continue; | ||
err = arg | ||
status = err.status || err.statusCode || status | ||
continue | ||
} | ||
switch (typeof arg) { | ||
case 'string': | ||
msg = arg; | ||
break; | ||
msg = arg | ||
break | ||
case 'number': | ||
status = arg; | ||
break; | ||
status = arg | ||
break | ||
case 'object': | ||
props = arg; | ||
break; | ||
props = arg | ||
break | ||
} | ||
@@ -42,3 +68,3 @@ } | ||
// constructor | ||
var HttpError = exports[status] | ||
var HttpError = createError[status] | ||
@@ -50,3 +76,3 @@ if (!err) { | ||
: new Error(msg || statuses[status]) | ||
Error.captureStackTrace(err, httpError) | ||
Error.captureStackTrace(err, createError) | ||
} | ||
@@ -66,47 +92,78 @@ | ||
return err; | ||
}; | ||
return err | ||
} | ||
var HttpError = exports.HttpError = function HttpError() { | ||
throw new TypeError('cannot construct abstract class'); | ||
}; | ||
/** | ||
* Create HTTP error abstract base class. | ||
* @private | ||
*/ | ||
inherits(HttpError, Error); | ||
function createHttpErrorConstructor () { | ||
function HttpError () { | ||
throw new TypeError('cannot construct abstract class') | ||
} | ||
// create generic error objects | ||
var codes = statuses.codes.filter(function (num) { | ||
return num >= 400; | ||
}); | ||
inherits(HttpError, Error) | ||
codes.forEach(function (code) { | ||
var name = toIdentifier(statuses[code]) | ||
return HttpError | ||
} | ||
/** | ||
* Create a constructor for a client error. | ||
* @private | ||
*/ | ||
function createClientErrorConstructor (HttpError, name, code) { | ||
var className = name.match(/Error$/) ? name : name + 'Error' | ||
if (code >= 500) { | ||
var ServerError = function ServerError(msg) { | ||
var self = new Error(msg != null ? msg : statuses[code]) | ||
Error.captureStackTrace(self, ServerError) | ||
self.__proto__ = ServerError.prototype | ||
Object.defineProperty(self, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
value: className, | ||
writable: true | ||
}) | ||
return self | ||
} | ||
inherits(ServerError, HttpError); | ||
ServerError.prototype.status = | ||
ServerError.prototype.statusCode = code; | ||
ServerError.prototype.expose = false; | ||
exports[code] = | ||
exports[name] = ServerError | ||
return; | ||
function ClientError (message) { | ||
// create the error object | ||
var err = new Error(message != null ? message : statuses[code]) | ||
// capture a stack trace to the construction point | ||
Error.captureStackTrace(err, ClientError) | ||
// adjust the [[Prototype]] | ||
setPrototypeOf(err, ClientError.prototype) | ||
// redefine the error name | ||
Object.defineProperty(err, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
value: className, | ||
writable: true | ||
}) | ||
return err | ||
} | ||
var ClientError = function ClientError(msg) { | ||
var self = new Error(msg != null ? msg : statuses[code]) | ||
Error.captureStackTrace(self, ClientError) | ||
self.__proto__ = ClientError.prototype | ||
Object.defineProperty(self, 'name', { | ||
inherits(ClientError, HttpError) | ||
ClientError.prototype.status = code | ||
ClientError.prototype.statusCode = code | ||
ClientError.prototype.expose = true | ||
return ClientError | ||
} | ||
/** | ||
* Create a constructor for a server error. | ||
* @private | ||
*/ | ||
function createServerErrorConstructor (HttpError, name, code) { | ||
var className = name.match(/Error$/) ? name : name + 'Error' | ||
function ServerError (message) { | ||
// create the error object | ||
var err = new Error(message != null ? message : statuses[code]) | ||
// capture a stack trace to the construction point | ||
Error.captureStackTrace(err, ServerError) | ||
// adjust the [[Prototype]] | ||
setPrototypeOf(err, ServerError.prototype) | ||
// redefine the error name | ||
Object.defineProperty(err, 'name', { | ||
enumerable: false, | ||
@@ -117,14 +174,54 @@ configurable: true, | ||
}) | ||
return self | ||
return err | ||
} | ||
inherits(ClientError, HttpError); | ||
ClientError.prototype.status = | ||
ClientError.prototype.statusCode = code; | ||
ClientError.prototype.expose = true; | ||
exports[code] = | ||
exports[name] = ClientError | ||
return; | ||
}); | ||
// backwards-compatibility | ||
exports["I'mateapot"] = exports.ImATeapot | ||
inherits(ServerError, HttpError) | ||
ServerError.prototype.status = code | ||
ServerError.prototype.statusCode = code | ||
ServerError.prototype.expose = false | ||
return ServerError | ||
} | ||
/** | ||
* Populate the exports object with constructors for every error class. | ||
* @private | ||
*/ | ||
function populateConstructorExports (exports, codes, HttpError) { | ||
codes.forEach(function forEachCode (code) { | ||
var CodeError | ||
var name = toIdentifier(statuses[code]) | ||
switch (String(code).charAt(0)) { | ||
case '4': | ||
CodeError = createClientErrorConstructor(HttpError, name, code) | ||
break | ||
case '5': | ||
CodeError = createServerErrorConstructor(HttpError, name, code) | ||
break | ||
} | ||
if (CodeError) { | ||
// export the constructor | ||
exports[code] = CodeError | ||
exports[name] = CodeError | ||
} | ||
}) | ||
// backwards-compatibility | ||
exports["I'mateapot"] = exports.ImATeapot | ||
} | ||
/** | ||
* Convert a string of words to a JavaScript identifier. | ||
* @private | ||
*/ | ||
function toIdentifier (str) { | ||
return str.split(' ').map(function (token) { | ||
return token.slice(0, 1).toUpperCase() + token.slice(1) | ||
}).join('').replace(/[^ _0-9a-z]/gi, '') | ||
} |
{ | ||
"name": "http-errors", | ||
"description": "Create HTTP error objects", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -14,6 +14,11 @@ "contributors": [ | ||
"inherits": "2.0.1", | ||
"statuses": ">= 1.2.1 < 2" | ||
"setprototypeof": "1.0.1", | ||
"statuses": ">= 1.3.0 < 2" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0.4.2", | ||
"eslint": "2.10.2", | ||
"eslint-config-standard": "5.3.1", | ||
"eslint-plugin-promise": "1.1.0", | ||
"eslint-plugin-standard": "1.3.2", | ||
"istanbul": "0.4.3", | ||
"mocha": "1.21.5" | ||
@@ -25,2 +30,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint **/*.js", | ||
"test": "mocha --reporter spec --bail", | ||
@@ -27,0 +33,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", |
@@ -53,2 +53,48 @@ # http-errors | ||
#### List of all constructors | ||
|Status Code|Constructor Name | | ||
|-----------|-----------------------------| | ||
|400 |BadRequest | | ||
|401 |Unauthorized | | ||
|402 |PaymentRequired | | ||
|403 |Forbidden | | ||
|404 |NotFound | | ||
|405 |MethodNotAllowed | | ||
|406 |NotAcceptable | | ||
|407 |ProxyAuthenticationRequired | | ||
|408 |RequestTimeout | | ||
|409 |Conflict | | ||
|410 |Gone | | ||
|411 |LengthRequired | | ||
|412 |PreconditionFailed | | ||
|413 |PayloadTooLarge | | ||
|414 |URITooLong | | ||
|415 |UnsupportedMediaType | | ||
|416 |RangeNotSatisfiable | | ||
|417 |ExpectationFailed | | ||
|418 |ImATeapot | | ||
|421 |MisdirectedRequest | | ||
|422 |UnprocessableEntity | | ||
|423 |Locked | | ||
|424 |FailedDependency | | ||
|425 |UnorderedCollection | | ||
|426 |UpgradeRequired | | ||
|428 |PreconditionRequired | | ||
|429 |TooManyRequests | | ||
|431 |RequestHeaderFieldsTooLarge | | ||
|451 |UnavailableForLegalReasons | | ||
|500 |InternalServerError | | ||
|501 |NotImplemented | | ||
|502 |BadGateway | | ||
|503 |ServiceUnavailable | | ||
|504 |GatewayTimeout | | ||
|505 |HTTPVersionNotSupported | | ||
|506 |VariantAlsoNegotiates | | ||
|507 |InsufficientStorage | | ||
|508 |LoopDetected | | ||
|509 |BandwidthLimitExceeded | | ||
|510 |NotExtended | | ||
|511 |NetworkAuthenticationRequired| | ||
## License | ||
@@ -55,0 +101,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13066
178
113
3
6
1
+ Addedsetprototypeof@1.0.1
+ Addedsetprototypeof@1.0.1(transitive)
Updatedstatuses@>= 1.3.0 < 2