1.0.0-rc.18 • Published 3 months ago
@exobase/use-catch v1.0.0-rc.18
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
)
1.0.0-rc.18
3 months ago
1.0.0-rc.17
3 months ago
1.0.0-rc.16
3 months ago
1.0.0-rc.15
3 months ago
1.0.0-rc.13
1 year ago
1.0.0-rc.12
1 year ago
1.0.0-rc.11
1 year ago
1.0.0-rc.9
1 year ago
1.0.0-rc.8
1 year ago
1.0.0-rc.10
1 year ago
1.0.0-rc.7
1 year ago
1.0.0-rc.6
1 year ago
1.0.0-rc.5
1 year ago
1.0.0-rc.4
1 year ago
1.0.0-rc.3
1 year ago
1.0.0-rc.2
1 year ago
1.0.0-rc.1
1 year ago