2.1.1 • Published 3 years ago

http-errors-wrapper v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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

2.1.1

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago