import { isObject } from '../error/utils'; class UnknownCauseError extends Error { [key: string]: unknown; } export function getCauseFromUnknown(cause: unknown): Error | undefined { if (cause instanceof Error) { return cause; } const type = typeof cause; if (type === 'undefined' || type === 'function' || cause === null) { return undefined; } // Primitive types just get wrapped in an error if (type !== 'object') { return new Error(String(cause)); } // If it's an object, we'll create a synthetic error if (isObject(cause)) { const err = new UnknownCauseError(); for (const key in cause) { err[key] = cause[key]; } return err; } return undefined; }