Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/geminiwind/jsonapierrors
A collection of Json API Errors
https://github.com/geminiwind/jsonapierrors
json-api
Last synced: 3 months ago
JSON representation
A collection of Json API Errors
- Host: GitHub
- URL: https://github.com/geminiwind/jsonapierrors
- Owner: GeminiWind
- License: mit
- Created: 2019-09-26T08:32:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T11:26:30.000Z (about 2 years ago)
- Last Synced: 2024-10-13T02:51:13.143Z (4 months ago)
- Topics: json-api
- Language: TypeScript
- Size: 381 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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