http-errors-wrapper

2.1.1 • Public • Published

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)

License

MIT

Package Sidebar

Install

npm i http-errors-wrapper

Weekly Downloads

11

Version

2.1.1

License

MIT

Unpacked Size

13.6 kB

Total Files

18

Last publish

Collaborators

  • luisfuentech