# @rokuem/apierror

> a simple api error format

Latest version **1.2.2** (published 2018-11-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install @rokuem/apierror
pnpm add @rokuem/apierror
yarn add @rokuem/apierror
bun add @rokuem/apierror
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2018-11-04 |
| First published | 2018-11-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 2.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Mateus Amorim |
| Maintainers | rokuem |
| Keywords | api, error |

## Links

- npm: https://www.npmjs.com/package/@rokuem/apierror
- Homepage: https://github.com/manoma2304/-rokuem-apierror
- npm.io page: https://npm.io/package/@rokuem/apierror

## Dependencies (3)

- [uid](https://npm.io/package/uid.md) 0.0.2
- [chalk](https://npm.io/package/chalk.md) 2.4.1
- [debug](https://npm.io/package/debug.md) 4.1.0

## 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.2.2 (latest) — 2018-11-04
- 1.2.1 — 2018-11-04
- 1.2.0 — 2018-11-04
- 1.1.3 — 2018-11-04
- 1.1.2 — 2018-11-04
- 1.1.1 — 2018-11-04
- 1.0.2 — 2018-11-04
- 1.0.1 — 2018-11-04
- 1.0.0 — 2018-11-04

## README

# How to use:
- Install the package: ``` npm i -S @rokuem/apierror ```
- import it in your node app: ``` import ApiError from "@rokuem/apierror" ```
- use it as an error passing the name, statusCode and message: ``` new ApiError("userNotFound", 404, "the requested user couldn't be found!") ```
- use it on api responses: ``` res.status(err.statusCode).json(err) ```
- by default the response doesn't show the message of an error in the json, so i placed another key "error" to handle that
- enable debug: ``` process.env.DEBUG = "@rokuem/apierror*" ```
- the debug message will be red and will have an uid, but u can also pass a custom identifier as the 4th argument

- full example:
```
import ApiError from "@rokuem/apierror"

export default function (req, res) {
    new Promise((resolve, reject) => {
        const email = req.body.email;

        if ((email || "").length === 0) {
            return reject("noEmail", 400, "we couldn't find an e-mail in the request!")
        }

        if (!req.body.password) {
            return reject(new ApiError("noPassword", 400, "password is missing!", email))
        }
    }).catch(err => {
        res.status(err.statusCode).json(err);
    })
}
```

# Note:
- this is a very simple package developed for personal usage, but anyone is welcome to use it too.

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