http-errors
Advanced tools
Comparing version 1.2.8 to 1.3.0
@@ -0,1 +1,9 @@ | ||
2015-02-01 / 1.3.0 | ||
================== | ||
* Construct errors using defined constructors from `createError` | ||
* Fix error names that are not identifiers | ||
- `createError["I'mateapot"]` is now `createError.ImATeapot` | ||
* Set a meaningful `name` property on constructed errors | ||
2014-12-09 / 1.2.8 | ||
@@ -2,0 +10,0 @@ ================== |
47
index.js
@@ -5,2 +5,8 @@ | ||
function toIdentifier(str) { | ||
return str.split(' ').map(function (token) { | ||
return token.slice(0, 1).toUpperCase() + token.slice(1) | ||
}).join('').replace(/[^ _0-9a-z]/gi, '') | ||
} | ||
exports = module.exports = function httpError() { | ||
@@ -32,13 +38,24 @@ // so much arity going on ~_~ | ||
if (typeof status !== 'number' || !statuses[status]) status = 500; | ||
if (typeof status !== 'number' || !statuses[status]) { | ||
status = 500 | ||
} | ||
// constructor | ||
var HttpError = exports[status] | ||
if (!err) { | ||
// create error | ||
err = new Error(msg || statuses[status]) | ||
err = HttpError | ||
? new HttpError(msg) | ||
: new Error(msg || statuses[status]) | ||
Error.captureStackTrace(err, httpError) | ||
} | ||
err.expose = status < 500; | ||
if (!HttpError || !(err instanceof HttpError)) { | ||
// add properties to generic error | ||
err.expose = status < 500 | ||
err.status = err.statusCode = status | ||
} | ||
for (var key in props) err[key] = props[key]; | ||
err.status = err.statusCode = status; | ||
return err; | ||
@@ -53,2 +70,5 @@ }; | ||
codes.forEach(function (code) { | ||
var name = toIdentifier(statuses[code]) | ||
var className = name.match(/Error$/) ? name : name + 'Error' | ||
if (code >= 500) { | ||
@@ -59,2 +79,8 @@ var ServerError = function ServerError(msg) { | ||
self.__proto__ = ServerError.prototype | ||
Object.defineProperty(self, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
value: className, | ||
writable: true | ||
}) | ||
return self | ||
@@ -67,3 +93,3 @@ } | ||
exports[code] = | ||
exports[statuses[code].replace(/\s+/g, '')] = ServerError; | ||
exports[name] = ServerError | ||
return; | ||
@@ -76,2 +102,8 @@ } | ||
self.__proto__ = ClientError.prototype | ||
Object.defineProperty(self, 'name', { | ||
enumerable: false, | ||
configurable: true, | ||
value: className, | ||
writable: true | ||
}) | ||
return self | ||
@@ -84,4 +116,7 @@ } | ||
exports[code] = | ||
exports[statuses[code].replace(/\s+/g, '')] = ClientError; | ||
exports[name] = ClientError | ||
return; | ||
}); | ||
// backwards-compatibility | ||
exports["I'mateapot"] = exports.ImATeapot |
{ | ||
"name": "http-errors", | ||
"description": "Create HTTP error objects", | ||
"version": "1.2.8", | ||
"author": { | ||
"name": "Jonathan Ong", | ||
"email": "me@jongleberry.com", | ||
"url": "http://jongleberry.com", | ||
"twitter": "https://twitter.com/jongleberry" | ||
}, | ||
"version": "1.3.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
"contributors": [ | ||
"Alan Plum <me@pluma.io>", | ||
"Douglas Christopher Wilson <doug@somethingdoug.com>" | ||
], | ||
"license": "MIT", | ||
@@ -12,0 +11,0 @@ "repository": "jshttp/http-errors", |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8125
102
0