# @mersocarlin/api-error

> Named API errors with original status code and custom message.

Latest version **0.0.2** (published 2021-04-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mersocarlin/api-error
pnpm add @mersocarlin/api-error
yarn add @mersocarlin/api-error
bun add @mersocarlin/api-error
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2021-04-08 |
| First published | 2020-03-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Hemerson Carlin |
| Maintainers | mersocarlin |
| Keywords | error, exception |

## Links

- npm: https://www.npmjs.com/package/@mersocarlin/api-error
- Repository: https://github.com/mersocarlin/api-error
- Homepage: https://github.com/mersocarlin/api-error#readme
- Issues: https://github.com/mersocarlin/api-error/issues
- npm.io page: https://npm.io/package/@mersocarlin/api-error

## 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

- 0.0.2 (latest) — 2021-04-08
- 0.0.1 — 2020-03-20

## README

# @mersocarlin/api-error

Named API errors with original status code and custom message.

## Install

```bash
yarn add @mersocarlin/api-error
```

```bash
npm i @mersocarlin/api-error
```

## Errors included

- BadGateway
- BadRequest
- Conflict
- Forbidden
- GatewayTimeout
- InternalServerError
- MethodNotAllowed
- NotFound
- PaymentRequired
- RequestTimeout
- TooManyRequests
- Unauthorized

## Usage

```ts
import { ApiError, UnauthorizedError } from '@mersocarlin/api-error'


function login(credentials) {
  try {
    await doLogin(credentials)
  } catch (error) {
    throw new UnauthorizedError('Invalid credentials', error)
  }
}

function otherOperation() {
  try {
    ...
  } catch (error) {
    throw new ApiError('Something went wrong', 500, error)
  }
}

```

## API

### `ApiError`

| Prop          | Type   | Default  | Description                                               |
| ------------- | ------ | -------- | --------------------------------------------------------- |
| message       | String | ApiError | Message that describes the type of error                  |
| statusCode    | Number | 500      | Status code                                               |
| originalError | Object |          | Original error thrown (from try/catch, promise reject...) |

### `BadGatewayError`

| Prop          | Type   | Default     | Description                                               |
| ------------- | ------ | --------    | --------------------------------------------------------- |
| message       | String | Bad Gateway | Message that describes the type of error                  |
| error         | Object |             | Original error thrown (from try/catch, promise reject...) |

**Note**: Same applies for all other error types.

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