UNPKG

errgo-ts

Version:

A lightweight error handling library inspired by Go and Rust.

27 lines (26 loc) 1.04 kB
/** * Executes a provided action, catching and re-throwing any errors with the provided additional context. * This function mimics Go's fmt.errorf wrapping pattern and preserves the original error * in the cause chain. Supports both synchronous and asynchronous actions. * * @param errorContext - descriptive message to prefix re-thrown errors * @param action - the action to execute that may throw an error * @returns `action`'s return value * @throws errors thrown by `action` wrapped in the provided context * @example * // Instead of a verbose try-catch block... * let data; * try { * data = getData(); * } catch (e) { * throw new Error("Failed to get data", { cause: e }); * } * * // ...use propagateError for more declarative code * const data = propagateError("Failed to get data", () => { * return getData(); * }); * */ export declare function propagateError<T>(errorContext: string, action: () => T): T; export declare function propagateError<T>(errorContext: string, action: () => Promise<T>): Promise<T>;