http-errors
Advanced tools
Comparing version 1.6.3 to 1.7.0
@@ -0,1 +1,8 @@ | ||
2018-07-30 / 1.7.0 | ||
================== | ||
* Set constructor name when possible | ||
* Use `toidentifier` module to make class names | ||
* deps: statuses@'>= 1.5.0 < 2' | ||
2018-03-29 / 1.6.3 | ||
@@ -7,3 +14,3 @@ ================== | ||
* deps: setprototypeof@1.1.0 | ||
* deps: statuses@'>= 1.3.1 < 2' | ||
* deps: statuses@'>= 1.4.0 < 2' | ||
@@ -10,0 +17,0 @@ 2017-08-04 / 1.6.2 |
28
index.js
@@ -19,2 +19,3 @@ /*! | ||
var inherits = require('inherits') | ||
var toIdentifier = require('toidentifier') | ||
@@ -166,2 +167,3 @@ /** | ||
inherits(ClientError, HttpError) | ||
nameFunc(ClientError, className) | ||
@@ -214,2 +216,3 @@ ClientError.prototype.status = code | ||
inherits(ServerError, HttpError) | ||
nameFunc(ServerError, className) | ||
@@ -224,2 +227,16 @@ ServerError.prototype.status = code | ||
/** | ||
* Set the name of a function, if possible. | ||
* @private | ||
*/ | ||
function nameFunc (func, name) { | ||
var desc = Object.getOwnPropertyDescriptor(func, 'name') | ||
if (desc.configurable) { | ||
desc.value = name | ||
Object.defineProperty(func, 'name', desc) | ||
} | ||
} | ||
/** | ||
* Populate the exports object with constructors for every error class. | ||
@@ -254,12 +271,1 @@ * @private | ||
} | ||
/** | ||
* 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.6.3", | ||
"version": "1.7.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -16,12 +16,13 @@ "contributors": [ | ||
"setprototypeof": "1.1.0", | ||
"statuses": ">= 1.4.0 < 2" | ||
"statuses": ">= 1.5.0 < 2", | ||
"toidentifier": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "4.18.1", | ||
"eslint": "4.19.1", | ||
"eslint-config-standard": "11.0.0", | ||
"eslint-plugin-import": "2.9.0", | ||
"eslint-plugin-import": "2.13.0", | ||
"eslint-plugin-markdown": "1.0.0-beta.6", | ||
"eslint-plugin-node": "6.0.1", | ||
"eslint-plugin-promise": "3.6.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"eslint-plugin-promise": "3.8.0", | ||
"eslint-plugin-standard": "3.1.0", | ||
"istanbul": "0.4.5", | ||
@@ -28,0 +29,0 @@ "mocha": "1.21.5" |
@@ -38,4 +38,2 @@ # http-errors | ||
All errors inherit from JavaScript `Error` and the exported `createError.HttpError`. | ||
### Error Properties | ||
@@ -56,2 +54,5 @@ | ||
Create a new error object with the given message `msg`. | ||
The error object inherits from `createError.HttpError`. | ||
<!-- eslint-disable no-undef, no-unused-vars --> | ||
@@ -67,4 +68,32 @@ | ||
### createError([status], [error], [properties]) | ||
Extend the given `error` object with `createError.HttpError` | ||
properties. This will not alter the inheritance of the given | ||
`error` object, and the modified `error` object is the | ||
return value. | ||
<!-- eslint-disable no-redeclare, no-undef, no-unused-vars --> | ||
```js | ||
fs.readFile('foo.txt', function (err, buf) { | ||
if (err) { | ||
if (err.code === 'ENOENT') { | ||
var httpError = createError(404, err, { expose: false }) | ||
} else { | ||
var httpError = createError(500, err) | ||
} | ||
} | ||
}) | ||
``` | ||
- `status` - the status code as a number | ||
- `error` - the error object to extend | ||
- `properties` - custom properties to attach to the object | ||
### new createError\[code || name\](\[msg]\)) | ||
Create a new error object with the given message `msg`. | ||
The error object inherits from `createError.HttpError`. | ||
<!-- eslint-disable no-undef, no-unused-vars --> | ||
@@ -71,0 +100,0 @@ |
16955
215
165
5
+ Addedtoidentifier@1.0.0
+ Addedtoidentifier@1.0.0(transitive)
Updatedstatuses@>= 1.5.0 < 2