UNPKG

2.73 kBMarkdownView Raw
1# HTTP Errors Wrapper
2
3This module allows you to throw custom and specific http-errors when handling server responses in your NodeJS APIs.
4
5<a href="https://nodei.co/npm/http-errors-wrapper">
6 <img src="https://nodei.co/npm/http-errors-wrapper.png?downloads=true">
7</a>
8
9[![npm version](https://img.shields.io/npm/v/http-errors-wrapper.svg?style=flat-square)](https://badge.fury.io/js/http-errors-wrapper)
10[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/LuisFuenTech/http-errors-wrapper/blob/master/LICENSE)
11[![NodeJS](https://img.shields.io/badge/node-8.x.x-brightgreen?style=flat-square)](https://github.com/LuisFuenTech/http-errors-wrapper/blob/master/package.json)
12[![install size](https://packagephobia.now.sh/badge?p=http-errors-wrapper)](https://packagephobia.now.sh/result?p=http-errors-wrapper)
13<!-- [![npm downloads](https://img.shields.io/npm/dm/http-errors-wrapper.svg?style=flat-square)](http://npm-stat.com/charts.html?package=http-errors-wrapper) -->
14
15# Compatibility
16
17The minimum supported version of Node.js is v8.
18
19# Usage
20
21## Installation
22
23```bash
24$ npm i http-errors-wrapper
25```
26
27## Test
28
29Run from the root folder:
30
31```bash
32$ npm run test
33```
34
35## Importing
36
37```js
38const HttpErrors = require('http-errors-wrapper');
39```
40
41## Example
42
43```js
44const HttpErrors = require('http-errors-wrapper');
45
46try {
47 throw new HttpErrors.notFoundError('User not found');
48}
49catch(error) {
50 if(error.isHttpError){
51 const { statusCode, message } = error;
52 return res.status(statusCode).send({ statusCode, message });
53 }
54}
55
56```
57
58## Error Object
59
60Each http error from this module has:
61
62- `date`: Date when the error where thrown with format `YYYY-MM-DD HH:mm:ss`
63- `isHttpError`: Flag to indicate the error belongs to this module in order to handle it
64- `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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
65- `name`: The default http error name
66- `statusCode`: The default http status code
67- `stack`: Error stack trace where was thrown
68
69## Methods
70
71- `badRequestError`: Handles 400 http error
72- `unauthorizedError`: Handles 401 http error
73- `forbiddenError`: Handles 403 http error
74- `notFoundError`: Handles 404 http error
75- `methodNotAllowedErrorError`: Handles 405 http error
76- `conflictError`: Handles 409 http error
77- `unsupportedMediaTypeError`: Handles 415 http error
78- `internalServerError`: Handles 500 http error
79- `badGatewayError`: Handles 502 http error
80- (`The rest will be added on demand`)
81
82# License
83
84[MIT](https://github.com/LuisFuenTech/http-errors-wrapper/blob/master/LICENSE)