import { Exception } from '@api-client/core/exceptions/exception.js'
/**
 * Converts a standard Error object into an Exception instance.
 * This function extracts relevant properties from the Error object
 * and sets them on the Exception instance, allowing for a more
 * consistent error handling experience across the application.
 * @param error The Error object to convert.
 * @returns An Exception instance with properties set from the Error object.
 */
export function fromError(error: Error): Exception {
  // The SDK error most likely is an instance of an Exception, so we can process it as one.
  // If not (some other error), we create a new Exception instance.
  const e = new Exception(error.message || 'An error occurred')
  const typed = error as unknown as Record<string, string | number>
  if (typed.code) {
    e.code = typed.code as string
  }
  if (typed.status) {
    e.status = Number(typed.status)
  }
  if (typed.help) {
    e.setHelp(typed.help as string)
  }
  if (typed.name) {
    e.name = typed.name as string
  }
  return e
}
