# json-api-error

> JSON API Errors

Latest version **1.1.0** (published 2020-05-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-api-error
pnpm add json-api-error
yarn add json-api-error
bun add json-api-error
```

## 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.1.0 |
| Published | 2020-05-06 |
| First published | 2019-09-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 178.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Gemini Wind |
| Maintainers | hai.dinh |
| Keywords | errors, json-api-errors, json-api-error, json-api |

## Links

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

## Dependencies (3)

- [typescript](https://npm.io/package/typescript.md) ^3.6.3
- [aggregate-error](https://npm.io/package/aggregate-error.md) ^3.0.1
- [ts-custom-error](https://npm.io/package/ts-custom-error.md) ^3.1.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2020-05-06
- 1.0.6 — 2019-11-29
- 1.0.5 — 2019-10-29
- 1.0.4 — 2019-10-23
- 1.0.3 — 2019-10-21
- 1.0.2 — 2019-10-08
- 1.0.1 — 2019-10-01
- 1.0.0 — 2019-09-27

## README

# **JSON API Errors**

JSON API Errors is a collection of popular errors which is format under of JSON API Specification. More detailed in [here](https://jsonapi.org/format/#error-objects)

## Getting Started

### Installation
The easiest way to install `json-api-error` is using NPM. If you have Node.js installed, it is most likely that you have NPM installed as well.

```
$ npm install json-api-error
```

### Usage

At the current implementation, `json-api-error` support the following common errors

- `BadRequestError`
- `UnauthorizedError`
- `MalformedError`
- `ForbiddenError`
- `NotFoundError`
- `MethodNotAllowedError`
- `NotAcceptableError`
- `RequestTimeoutError`
- `UnsupportedMediaTypeError`
- `InternalError`
- `NotImplementedError`
- `BadGatewayError`
- `ServiceUnavailableError`
- `GatewayTimeoutError`

### Example

### Error usage with simple detailed message

```js
import { BadRequestError } from 'json-api-error';

throw new BadRequestError('Request is invalid');
```

### Error usage with options

```
import { BadRequestError } from 'json-api-error';

throw new BadRequestError({
    id: 'BadRequestError',
    code: 'BadRequestError',
    title: 'BadRequestError',
    detail: 'The #/userName must be number'
}));
```

### Error usage with customized your JsonApiError

```js
import JsonApiError from 'json-api-error';

throw new JsonApiError({
    id: 'RequestEntityTooLargeError',
    status: '413',
    code: 'RequestEntityTooLargeError',
    title: 'RequestEntityTooLargeError',
    detail: 'Request Entity Too Large'
  });
```

### AggregateJsonApiError(errors, status)

```js
import { AggregateJsonApiError } from 'json-api-error';

throw new AggregateJsonApiError([
  new BadRequestError('Something went wrong'),
  new BadRequestError('Request is valid')
], 400);
```

### Express Middleware - JSON API Error Handler usage

`json-api-error` also ships a Express middleware to handle these JSON API Errors. This middleware will catch JSON API Error and return response for your end-user as the following example format (including the status response):

```js
{
  errors: [{
    id: 'NotFoundError',
    status: '404',
    code: 'NotFoundError',
    title: 'NotFoundError',
    detail: 'Resource was not found'
  }]
}
```

To use this, configure your Express app


```js
import { jsonApiErrorHandler } from 'json-api-error/middlewares';


app.use(jsonApiErrorHandler);

```

**Note:** For best practice, please place `jsonApiErrorHandler` below utilized middlewares.

### Options

| Properties  | Detail   | Type  |  Default Value  |
|---|---|---|---|
| id  | a unique identifier for this particular occurrence of the problem  | string  |  equal to error name |
| links  | a links object, more detailed in [here](https://jsonapi.org/format/#error-objects) |  object | N/A |
| status | the HTTP status code applicable to this problem, expressed as a string value  |  string | equal to HTTP code |
| code |  an application-specific error code, expressed as a string value | string  | equal to error name |
| title | a short, human-readable summary of the problem  | string  |  equal to error name |
| detail | a human-readable explanation specific to this occurrence of the problem  | string  |  equal to error name |
| source |  an object containing references to the source of the error, more detailed in [here](https://jsonapi.org/format/#error-objects) |  object | N/A |
| meta | a meta object containing non-standard meta-information about the error.  | object  |  N/A |

## LICENSE

MIT

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