/** * Coerces an unknown thrown value to an `Error` instance. * Returns the value as-is when it is already an `Error`; otherwise wraps it with `String(value)`. * * @example * ```ts * try { ... } catch(err) { * throw new Error('Build failed', { cause: toError(err) }) * } * ``` */ export function toError(value: unknown): Error { return value instanceof Error ? value : new Error(String(value)) } /** * Extracts a human-readable message from any thrown value. * * @example * ```ts * getErrorMessage(new Error('oops')) // 'oops' * getErrorMessage('plain string') // 'plain string' * ``` */ export function getErrorMessage(value: unknown): string { return value instanceof Error ? value.message : String(value) } /** * Returns `error` followed by every error in its `cause` chain, outermost first. Stops at a * non-`Error` cause or a cycle. * * @example * ```ts * getErrorChain(new Error('Config failed loading', { cause: new Error('boom') })).map((error) => error.message) * // ['Config failed loading', 'boom'] * ``` */ export function getErrorChain(error: unknown): Array { const chain: Array = [] for (let current = error; current instanceof Error && !chain.includes(current); current = current.cause) { chain.push(current) } return chain } /** * Builds an error that `Diagnostics.isError` from `@kubb/core` reads as a `Diagnostics.Error` with * `code`, without importing `@kubb/core` at run time. Use it in packages that only take core as a * type or peer dependency, so reporters show the code and `help` instead of `KUBB_UNKNOWN`. * * @example * ```ts * throw createDiagnosticError({ code: 'KUBB_MODULE_NOT_FOUND', message: 'Install typescript', cause }) * ``` */ export function createDiagnosticError({ code, message, help, cause }: { code: `KUBB_${string}`; message: string; help?: string; cause?: Error }): Error { const error = new Error(message, { cause }) error.name = 'DiagnosticError' return Object.assign(error, { diagnostic: { code, severity: 'error' as const, message, ...(help ? { help } : {}), ...(cause ? { cause } : {}) } }) }