Constructors for HTTP errors
npm install http-error-constructorConstructors for HTTP errors


Note: This module will only work with Node.js >= 4.0.
``sh`
npm install http-error-constructor
is UpperCamelCased status name (description)
$3
Parameters
*
[statusCode=500] {Number} - Three-digit integer code. Status Code Registry
* [messageOrProperties] {String | Object} - String message or object with additional properties
* [properties] {Object} - Additional properties (used only if messageOrProperties is string)$3
`js
var HttpError = require('http-error-constructor');var err = new HttpError(400);
// err.name - 'BadRequest'
// err.statusCode - 400
// err.status - 400
// err.message - 'Bad Request'
// name, statusCode and status are non-enumerable and therefore ignored in JSON.stringify(err):
// '{"message": "Bad Request"}'
`With custom message and additional properties:
`js
var err = new HttpError(400, 'Validation Failed', {
fields: {
phoneNumber: 'Invalid format'
}
});/* or you can include message in the properties object
var err = new HttpError(400, {
message: 'Validation Failed',
fields: {
phoneNumber: 'Invalid format'
}
});
*/
// err.statusCode - 400
// err.status - 400
// err.message - 'Validation Failed'
// err.fields - { phoneNumber: 'Invalid format' }
// JSON.stringify(err) - '{"message": "Bad Request", "fields": { "phoneNumber": "Invalid format" } }'
`Using specific error constructor:
`js
var properties = {
message: 'Validation Failed',
fields: {
phoneNumber: 'Invalid format'
}
};var err = new HttpError.BadRequest(properties); // or new HttpError400
// err instanceof HttpError - true
// err instanceof HttpError.BadRequest - true
// err instanceof HttpError[400] - true
`You can use custom status codes:
`js
var err = new HttpError(471, 'Custom Error');// err.name - '471 Error'
// err.statusCode - 471
// err.status - 471
// err.message - 'Custom Error'
`Tests
`sh
npm install
npm test
``