This module allows you to throw custom and specific http-errors when handling server responses in your NodeJS APIs.
The minimum supported version of Node.js is v10.
$ npm i http-errors-wrapper
Run from the root folder:
$ npm run test
const HttpErrors = require('http-errors-wrapper');
const HttpErrors = require('http-errors-wrapper');
try {
throw new HttpErrors.notFoundError('User not found');
} catch (error) {
if (error.isHttpError) {
const { statusCode, message } = error;
return res.status(statusCode).send({ statusCode, message });
}
}
Each http error from this module has:
date
: Date when the error where thrown with formatYYYY-MM-DD HH:mm:ss
isHttpError
: Flag to indicate the error belongs to this module in order to handle itmessage
: Custom message to send with the error. A short description to resume what happened. By default is the error name provided by MDN Web Docsname
: The default http error namestatusCode
: The default http status codestack
: Error stack trace where was thrown
badRequestError
: Handles 400 http errorunauthorizedError
: Handles 401 http errorforbiddenError
: Handles 403 http errornotFoundError
: Handles 404 http errormethodNotAllowedError
: Handles 405 http errorconflictError
: Handles 409 http errorunsupportedMediaTypeError
: Handles 415 http errorinternalServerError
: Handles 500 http errorbadGatewayError
: Handles 502 http error- (
The rest will be added on demand
)
If you want to add something or fix it, please follow this guide