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: 8 months 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 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-10T16:03:12.000Z (over 8 years ago)
- Last Synced: 2025-01-07T09:04:39.034Z (9 months 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
```