Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/request / src / deps / toPrettyError.ts
Size: Mime:
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
}