UNPKG

@playerony/try-catch-wrapper

Version:
47 lines (46 loc) 1.19 kB
"use strict"; /** * @packageDocumentation Functional try-catch wrapper. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncTryCatchWrapper = exports.tryCatchWrapper = void 0; /** * try-catch wrapper for synchronous functions * * @param toExecute - synchronous function to execute inside of try-catch * @param onError - synchronous function to execute when an error occurs * * @public */ const tryCatchWrapper = (toExecute, onError) => { try { return toExecute(); } catch (error) { if (onError) { // @ts-expect-error onError(error); } } }; exports.tryCatchWrapper = tryCatchWrapper; /** * try-catch wrapper for asynchronous functions * * @param toExecute - asynchronous function to execute inside of try-catch * @param onError - asynchronous function to execute when an error occurs * * @public */ const asyncTryCatchWrapper = async (toExecute, onError) => { try { return await toExecute(); } catch (error) { if (onError) { // @ts-expect-error onError(error); } } }; exports.asyncTryCatchWrapper = asyncTryCatchWrapper;