Skip to content

Catch errors and declaratively map them to other errors or values.

Notifications You must be signed in to change notification settings

sebinsua/catch-to

Repository files navigation

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

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<ErrorCategory>, log?: LogSignature): CatchSignature

CatchSignature: (error: Error): Error|any

Types

ErrorCategory: Array<{ on: Error|Array<Error>|ErrorPredicateSignature, toError?: Error|ToErrorSignature, toValue?: any|ToValueSignature }>

ErrorPredicateSignature: (error: Error): boolean

ToErrorSignature: (error: Error): Error

ToValueSignature: (error: Error): any

LogSignature: (message?: string): void

Install

yarn add catch-to

About

Catch errors and declaratively map them to other errors or values.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published