Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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
```