A small utility for generating consistent errors
npm
npm install @dancastillo/error
yarn
npm add @dancastillo/error
pnpm
pnpm install @dancastillo/error
createError(code, message [, detail [, meta]])
code
(number
, required) - The error code, you can access it viaerror.code
.message
(string
, required) - The error message.detail
(string[]
, optional) - The error detail. You can include additional information about the error that has occurred.meta
(T extends object
, optional) - Additional metadata object that you can pass in with generic.
import { createError, type DCError } from '@dancastillo/error'
type Meta = { userId: string }
const err = createError<Meta>(500, 'Title', 'Detail', { userId: 'some-user-id' })
console.log(err.code) // 500
console.log(err.title) // 'Title'
console.log(err.detail) // 'Detail'
console.log(err.meta) // { userId: 'some-user-id' }
import { createError } from '@dancastillo/error'
const err = createError(500, 'Title', 'Detail', { userId: 'some-user-id' })
console.log(err.code) // 500
console.log(err.title) // 'Title'
console.log(err.detail) // 'Detail'
console.log(err.meta) // { userId: 'some-user-id' }
Licensed under MIT.