# heror

> HTTP Error Classes

Latest version **1.0.0** (published 2019-07-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install heror
pnpm add heror
yarn add heror
bun add heror
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-07-26 |
| First published | 2019-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | ozylog |
| Keywords | error, http, typescript |

## Links

- npm: https://www.npmjs.com/package/heror
- Repository: git@github.com:ozylog/heror
- Homepage: https://github.com/ozylog/heror.git#readme
- Issues: https://github.com/ozylog/heror/issues
- npm.io page: https://npm.io/package/heror

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-07-26
- 0.1.0 — 2019-07-17

## README

# heror
![Build](https://badgen.net/travis/ozylog/heror/master)
![Coverage](https://badgen.net/coveralls/c/github/ozylog/heror/master)
![DevDependencies](https://badgen.net/david/dev/ozylog/heror)
![LatestVersion](https://badgen.net/npm/v/heror)
![License](https://badgen.net/npm/license/heror)

HTTP error classes

## Install
```
yarn add heror
```

## Class
```
new BadRequestError(message: string = 'Bad Request', data?: any)
```
- `message` - error message
- `data` - optional input that you can use for additional information regarding the error

## Function
```
createError(statusCode: number, error: string)
```
- `statusCode` - http error status code
- `error` - official http error type/message

## List of error classes
We provide some common http errors below. If your case is not covered by the errors. You could create new error using `createError` function. Please see [usage examples](#usage-examples).

| Status Code | Error Class Name         |
|-------------|--------------------------|
| 400         | BadRequestError          |
| 401         | UnauthorizedError        |
| 403         | ForbiddenError           |
| 404         | NotFoundError            |
| 405         | MethodNotAllowedError    |
| 406         | NotAcceptableError       |
| 408         | RequestTimeoutError      |
| 409         | ConflictError            |
| 412         | PreconditionFailedError  |
| 422         | UnprocessableEntityError |
| 500         | InternalServerError      |
| 501         | NotImplementedError      |
| 503         | ServiceUnavailableError  |
| 507         | InsufficientStorageError |
| 508         | NotExtendedError         |

## Usage Examples
```
import { BadRequestError, UnauthorizedError, createError } from 'heror';

const URITooLongError = createError(414, 'URI Too Long');

async function userController(req, res, next) {
  if (!req.auth) {
    throw new UnauthorizedError('Authentication is required', {
      expose: true,
      details: {}
    });
  }
  /*
    it will throw error instance
    {
      statusCode: 401,
      error: 'Unauthorized',
      message: 'Authentication is required',
      data: {
        expose: true,
        details: {}
      },
      ...nativeErrorProperties
    }
  /*

  if (!req.params.id) throw new BadRequestError();
  /*
    it will throw error instance
    {
      statusCode: 400,
      error: 'Bad Request',
      message: 'Bad Request',
      ...nativeErrorProperties
    }
  */

  if (req.uri.length > 100) {
    throw new URITooLongError('Max URI Length is 100 characters', {
      uriLength: req.uri.length
    });
  }
  /*
    it will throw error instance
    {
      statusCode: 414,
      error: 'URI Too Long',
      message: 'Max URI Length is 100 characters',
      data: {
        uriLength: 111
      },
      ...nativeErrorProperties
    }
  /*

}

```

## License
MIT

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