mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
25 lines
488 B
JavaScript
25 lines
488 B
JavaScript
//#region src/function/try-catch.ts
|
|
/**
|
|
* Wrap a function to catch and handle errors instead of throwing.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const safeFn = tryCatch(riskyFn, (e) => logger.error(e));
|
|
* safeFn?.(); // Never throws
|
|
* ```
|
|
*/
|
|
function tryCatch(fn, onError = console.error) {
|
|
if (!fn) return void 0;
|
|
return ((...args) => {
|
|
try {
|
|
return fn(...args);
|
|
} catch (error) {
|
|
onError(error);
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
//#endregion
|
|
export { tryCatch };
|
|
|
|
//# sourceMappingURL=try-catch.js.map
|