Repository URL to install this package:
|
Version:
7.1.2-patch-delete ▾
|
export type PrettyErrorType = Error & {
code?: number | string
param?: any
}
/**
* @todo rewrite this, has side effects
* @description attempt to use better and human-friendly error messages
*/
export function toPrettyError(obj: Error | Response) {
let prettyError: PrettyErrorType
const objAsAny = obj as any
const bodyAsError = objAsAny.body as PrettyErrorType
const bodyAsArray = objAsAny.body as { error: PrettyErrorType }
if (
typeof bodyAsError === 'object' &&
typeof bodyAsError.message === 'string'
) {
prettyError = new Error(bodyAsError.message)
} else if (
!Array.isArray(objAsAny.body) &&
// attempt to utilize Stripe-inspired error messages
typeof bodyAsArray.error === 'object'
) {
const bodyError = bodyAsArray.error
if (bodyError.message) {
prettyError = new Error(bodyError.message)
}
if (bodyError.stack) {
prettyError.stack = bodyError.stack
}
if (bodyError.code) {
prettyError.code = bodyError.code
}
if (bodyError.param) {
prettyError.param = bodyError.param
}
}
return prettyError
}