mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
25 lines
549 B
TypeScript
25 lines
549 B
TypeScript
/**
|
|
* Wrap a function to catch and handle errors instead of throwing.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const safeFn = tryCatch(riskyFn, (e) => logger.error(e));
|
|
* safeFn?.(); // Never throws
|
|
* ```
|
|
*/
|
|
export function tryCatch<T extends (...args: any[]) => unknown>(
|
|
fn: T | undefined,
|
|
onError: (error: unknown) => void = console.error,
|
|
): T | undefined {
|
|
if (!fn) return undefined;
|
|
|
|
return ((...args: Parameters<T>) => {
|
|
try {
|
|
return fn(...args);
|
|
} catch (error) {
|
|
onError(error);
|
|
return undefined;
|
|
}
|
|
}) as T;
|
|
}
|