http-errors-wrapper
v2.1.1
Published
Handle http errors in an easy way
Downloads
96
Maintainers
Readme
HTTP Errors Wrapper
This module allows you to throw custom and specific http-errors when handling server responses in your NodeJS APIs.
Compatibility
The minimum supported version of Node.js is v10.
Usage
Installation
$ npm i http-errors-wrapper
Test
Run from the root folder:
$ npm run test
Importing
const HttpErrors = require('http-errors-wrapper');
Example
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 });
}
}
Error Object
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
Methods
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
)