Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sebinsua/catch-to
Catch errors and declaratively map them to other errors or values.
https://github.com/sebinsua/catch-to
api boom catch errors map promise value
Last synced: 5 days ago
JSON representation
Catch errors and declaratively map them to other errors or values.
- Host: GitHub
- URL: https://github.com/sebinsua/catch-to
- Owner: sebinsua
- Created: 2017-03-20T19:12:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-04-10T16:03:12.000Z (over 7 years ago)
- Last Synced: 2024-10-18T08:35:50.980Z (28 days ago)
- Topics: api, boom, catch, errors, map, promise, value
- Language: JavaScript
- Size: 50.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# `catch-to`
> Catch errors and declaratively map them to other errors or values.This is useful when you do not wish to expose the specific errors your system
uses to describe its internal state, and instead want to respond with more general, human-like errors.## Example
```js
const Boom = require('boom')
const toErrors = require('catch-to')(Boom.badImplementation)const login = require('./login')
const {
UnauthorizedLoginError,
AccountLockedError,
TooManyLoginsError,
MissingAuthenticityTokenError
} = require('./errors')login('username', 'password')
.catch(
toErrors([
{
on: [ UnauthorizedLoginError, MissingAuthenticityTokenError ],
toError: err => Boom.unauthorized(err)
},
{
on: TooManyLoginsError
toError: Boom.badRequest()
},
{
on: AccountLockedError,
toError: Boom.locked()
}
])
)
```## API
### `CreateCatchToSignature`: `(fallbackError?: ToErrorSignature, defaultLog?: LogSignature): CatchToSignature`
### `CatchToSignature`: `(errorCategories?: ErrorCategory|Array, log?: LogSignature): CatchSignature`
### `CatchSignature`: `(error: Error): Error|any`
### Types
#### `ErrorCategory`: `Array<{ on: Error|Array|ErrorPredicateSignature, toError?: Error|ToErrorSignature, toValue?: any|ToValueSignature }>`
#### `ErrorPredicateSignature`: `(error: Error): boolean`
#### `ToErrorSignature`: `(error: Error): Error`
#### `ToValueSignature`: `(error: Error): any`
#### `LogSignature`: `(message?: string): void`
## Install
```sh
yarn add catch-to
```