@exobase/use-catch
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-rc.18 • Public • Published

title: 'useCatch' description: 'A hook to handle errors' group: 'Hooks' badge: 'Error Handling'

An Exobase hook that catches errors and gives you a chance to log them, map them, and return something user friendly.

Install

yarn add @exobase/use-catch
# or
yarn add @exobase/hooks

Import

import { useCatch } from '@exobase/use-catch'
// or
import { useCatch } from '@exobase/hooks'

Usage

You can add the useCatch hook anywhere, when an error is thrown below the hook your callback will be called with the error. What your callback returns will become the new response.

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useExpress } from '@exobaes/use-express'
import { useCors } from '@exobase/use-cors'
import { useCatch } from '@exobase/use-catch'

const endpoint = (props: Props) => {
  throw new DatabaseSecretError('The given secret password1 was incorrect')
}

export default compose(
  useExpress(),
  useCatch((props, error) => {
    console.error(error)
    switch (error.name) {
      'EntityNotFound':
        return {
          ...props.response,
          status: 404,
          body: { message: 'The item was not found' }
        }
    }
    return {
      ...props.response,
      status: 500,
      body: { message: 'Unknown error' }
    }
  })
  useCors(),
  endpoint
)

Readme

Keywords

none

Package Sidebar

Install

npm i @exobase/use-catch

Weekly Downloads

17

Version

1.0.0-rc.18

License

MIT

Unpacked Size

8.62 kB

Total Files

17

Last publish

Collaborators

  • rayepps