http-error-constructor
Constructors for HTTP errors
Note: This module will only work with Node.js >= 4.0.
Installation
npm install http-error-constructor
Usage
new HttpError([statusCode=500], [messageOrProperties], [properties])
new HttpError.<StatusName>([messageOrProperties], [properties])
StatusName
is UpperCamelCased status name (description)
new HttpError[<statusCode>]([messageOrProperties], [properties])
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)
Examples
var HttpError = require('http-error-constructor');
var err = new HttpError(400);
With custom message and additional properties:
var err = new HttpError(400, 'Validation Failed', {
fields: {
phoneNumber: 'Invalid format'
}
});
Using specific error constructor:
var properties = {
message: 'Validation Failed',
fields: {
phoneNumber: 'Invalid format'
}
};
var err = new HttpError.BadRequest(properties);
You can use custom status codes:
var err = new HttpError(471, 'Custom Error');
Tests
npm install
npm test
License
MIT