FlavioAandres / http-errors-wrapper

Handle http errors in an easy way

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP Errors Wrapper

This module allows you to throw custom and specific http-errors when handling server responses in your NodeJS APIs.

npm version MIT License NodeJS install size npm downloads

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 format YYYY-MM-DD HH:mm:ss
  • isHttpError: Flag to indicate the error belongs to this module in order to handle it
  • message: Custom message to send with the error. A short description to resume what happened. By default is the error name provided by MDN Web Docs
  • name: The default http error name
  • statusCode: The default http status code
  • stack: Error stack trace where was thrown

Methods

  • badRequestError: Handles 400 http error
  • unauthorizedError: Handles 401 http error
  • forbiddenError: Handles 403 http error
  • notFoundError: Handles 404 http error
  • methodNotAllowedError: Handles 405 http error
  • conflictError: Handles 409 http error
  • unsupportedMediaTypeError: Handles 415 http error
  • internalServerError: Handles 500 http error
  • badGatewayError: Handles 502 http error
  • (The rest will be added on demand)

Contributing

If you want to add something or fix it, please follow this guide

License

MIT

About

Handle http errors in an easy way

License:MIT License


Languages

Language:JavaScript 100.0%