
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
http-error-constructor
Advanced tools
Constructors for HTTP errors
Note: This module will only work with Node.js >= 4.0.
npm install http-error-constructor
StatusName
is UpperCamelCased status name (description)
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)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:
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:
var properties = {
message: 'Validation Failed',
fields: {
phoneNumber: 'Invalid format'
}
};
var err = new HttpError.BadRequest(properties); // or new HttpError[400](properties)
// err instanceof HttpError - true
// err instanceof HttpError.BadRequest - true
// err instanceof HttpError[400] - true
You can use custom status codes:
var err = new HttpError(471, 'Custom Error');
// err.name - '471 Error'
// err.statusCode - 471
// err.status - 471
// err.message - 'Custom Error'
npm install
npm test
FAQs
Constructors for HTTP errors
We found that http-error-constructor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.