Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mersocarlin/api-error
Named API errors with original status code and custom message.
https://github.com/mersocarlin/api-error
Last synced: 1 day ago
JSON representation
Named API errors with original status code and custom message.
- Host: GitHub
- URL: https://github.com/mersocarlin/api-error
- Owner: mersocarlin
- License: mit
- Created: 2020-03-20T09:12:19.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T00:37:55.000Z (almost 2 years ago)
- Last Synced: 2024-12-15T02:37:56.659Z (10 days ago)
- Language: TypeScript
- Homepage:
- Size: 881 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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.