# @onibi/errors

> Standardized error types/responses/handling for express.js based api's and webservers.

Latest version **2.0.1** (published 2023-12-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install @onibi/errors
pnpm add @onibi/errors
yarn add @onibi/errors
bun add @onibi/errors
```

## 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 | 2.0.1 |
| Published | 2023-12-02 |
| First published | 2022-08-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 31.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matthijs Reyers |
| Maintainers | matthijsr |

## Links

- npm: https://www.npmjs.com/package/@onibi/errors
- npm.io page: https://npm.io/package/@onibi/errors

## Recent versions

- 2.0.1 (latest) — 2023-12-02
- 2.0.0 — 2023-12-02
- 0.1.14 — 2023-07-08
- 0.1.13 — 2023-07-06
- 0.1.12 — 2023-07-06
- 0.1.10 — 2022-08-14
- 0.1.9 — 2022-08-12
- 0.1.8 — 2022-08-12
- 0.1.7 — 2022-08-12
- 0.1.6 — 2022-08-12
- 0.1.5 — 2022-08-12
- 0.1.4 — 2022-08-12
- 0.1.3 — 2022-08-09
- 0.1.2 — 2022-08-09
- 0.1.1 — 2022-08-09
- … 2 more at https://npm.io/package/@onibi/errors/versions

## README

# @onibi/errors

Standardized error types for most common HTTP error status codes that have a built-in `.toJSON` method that converts the error into a standardized JSON response.
Best used in conjunction with the `@onibi/errorhandler` for express.js that automatically catches any `HttpError` and returns correct status code/JSON response.

<br>

## JSON response
All errors contain a `.toJSON` method that converts the error into a standardized JSON response according to the interface below:
```ts
interface ApiErrorResponse {
    /** HTTP status code. */
    statusCode: number;

    /** Unique type id for this error type. */
    errorType: string;

    /** Generic name for this error type, may be shown to end users. */
    errorName: string;

    /** Error description for programmers/debugging, should NOT be shown to end users. */
    description: string;

    /** Error message that describes the error in more detail, may be shown to end users. */
    message: string;

    /** Error message is safe to show to users/user friendly. */
    isUserSafe: boolean;

    /** Stack trace for the error, must be explicitly added by the error handler and should never be populated in production environments. */
    stackTrace?: string;
}
```

<br>

## Usage example

```ts
import express from 'express';
import { Http404Error } from '@onibi/errors';
import errorHandler from '@onibi/errorhandler';

let app = express();

app.get('/404', (req, res) => {
    throw new Http404Error();
})

// The errorHandler should always be the last thing added to the express app.
app.user(errorHandler);

app.listen(8080);
```

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