A small utility for generating consistent errors
npm install @dancastillo/error
createError(code, message [, details [, statusCode [, error]]])
-
code
(string
, required) - The error code, you can access it viaerror.code
. For consistency, it is recommended using ErrorCode -
message
(string
, required) - The error message. This can be customized to use interpolated strings for formatting the message. -
details
(string[]
, optional) - The error details. You can include additional information about the error that has occurred. Default is an empty array. -
statusCode
(number
, optional) - The status code that will be used if you want to sent via HTTP. Note this will be automatically populated based on thecode
you pass in
import createError from '@dancastillo/error
const CreatedError = createError('INTERNAL_ERROR', 'Error happened')
const err = CreatedError()
console.log(err.code) // 'INTERNAL_ERROR'
console.log(err.message) // 'Error happened'
Licensed under MIT.