type Success = { data: T; error: null }; type Failure = { data: null; error: E }; export type Result = Success | Failure; /** * A universal tryCatch wrapper that: * - Never throws * - Always resolves to Result * - Allows optional error mapping function for strong typing */ export async function tryCatch( promise: Promise, onError?: (error: unknown) => E, ): Promise> { try { const data = await promise; return { data, error: null }; } catch (err: unknown) { const error = onError ? onError(err) : err instanceof Error ? (err as E) : (new Error(String(err)) as E); return { data: null, error }; } }