# @abs-warranty/http-errors

> Custom HTTP Errors

Latest version **2.0.4** (published 2023-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @abs-warranty/http-errors
pnpm add @abs-warranty/http-errors
yarn add @abs-warranty/http-errors
bun add @abs-warranty/http-errors
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2023-03-08 |
| First published | 2023-02-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=16 |
| Dependencies | 0 |
| Unpacked size | 18.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | sjlane, jsowardsabs, wtkersting, daverted |

## Links

- npm: https://www.npmjs.com/package/@abs-warranty/http-errors
- Repository: https://github.com/abs-warranty/http-errors
- Homepage: https://github.com/abs-warranty/http-errors#readme
- Issues: https://github.com/abs-warranty/http-errors/issues
- npm.io page: https://npm.io/package/@abs-warranty/http-errors

## Recent versions

- 2.0.4 (latest) — 2023-03-08
- 2.0.3 — 2023-03-01
- 2.0.2 — 2023-03-01
- 2.0.1 — 2023-02-23
- 2.0.0 — 2023-02-17
- 1.0.6 — 2023-02-10
- 1.0.5 — 2023-02-10
- 1.0.4 — 2023-02-08
- 1.0.3 — 2023-02-07

## README

[![Maintainability](https://api.codeclimate.com/v1/badges/37bec9bad4cb264a2f90/maintainability)](https://codeclimate.com/github/abs-warranty/http-errors/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/37bec9bad4cb264a2f90/test_coverage)](https://codeclimate.com/github/abs-warranty/http-errors/test_coverage)

# Custom HTTP Errors

## To Install:

`yarn add @abs-warranty/http-errors`

## Example Usage:

```javascript
const { NotFoundError } = require('@abs-warranty/http-errors')

throw new NotFoundError('<yourMessageHere>')
```

## Available Errors:

| Error                    | Status Code | Message               | Description                                                                 |
| ------------------------ | ----------- | --------------------- | --------------------------------------------------------------------------- |
| BadReqeustError          | 400         | bad request           | Custom error denoting a bad request was made                                |
| UnauthorizedError        | 401         | unauthorized          | Custom error denoting the user is not authorized                            |
| ForbiddenError           | 403         | forbidden             | Custom error denoting the user has insufficient privileges                  |
| NotFoundError            | 404         | not found             | Custom error denoting a resource has not been found                         |
| NotAllowedError          | 405         | method not allowed    | Custom error denoting the method used is not allowed for this endpoint      |
| ConflictError            | 409         | conflict              | Custom error denoting a resource is in conflict                             |
| IAmATeapotError          | 418         | i am a teapot         | Custom error denoting that the machine really is actually a teapot 🍵       |
| EnhanceYourCalmError     | 420         | enhance your calm     | Custom error denoting the user must enhance their frickin' calm, maaaan. 🌿 |
| UnprocessableEntityError | 422         | unprocessable entity  | Custom error denoting an unprocessable entity                               |
| InternalServerError      | 500         | internal server error | Custom error denoting an internal server error                              |

## Error Handler Usage:

```javascript
// Import and create express app
const express = require('express')
const app = express()
// Import HttpErrors middleware
const { NotFoundError } = require('@abs-warranty/http-errors')
const { errorHandler, notFoundHandler, axiosErrorHandler } = require('@abs-warranty/http-errors/middleware')

// All routes are placed here
app.get('/example', (req, res, next) => {
  throw new NotFoundError('Resource not found')
})

// Handle invalid endpoints
app.use(notFoundHandler)

// Axios error handler converts axios errors to http errors
app.use(axiosErrorHandler)

// Catch-all error handler must be used as last middleware in application
app.use(errorHandler)
```

> Note: These middleware must be placed after all routes have been added to express application

---
_Source: https://npm.io/package/@abs-warranty/http-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
