@rxap/utilities
Version:
A collection of utility functions, types and interfaces.
70 lines • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TryAndLogOnError = TryAndLogOnError;
exports.TryAndLogOnErrorAsync = TryAndLogOnErrorAsync;
const tslib_1 = require("tslib");
/**
* `TryAndLogOnError` is a higher-order function that wraps a provided function `fnc` in a try-catch block.
* If an error is thrown during the execution of `fnc`, the error message is logged to the console and a default result is returned.
*
* @template T The expected return type of the function `fnc`.
* @template A An array-like type representing the arguments of the function `fnc`.
*
* @param {(...args: A) => T} fnc The function to be wrapped in a try-catch block. This function is expected to take arguments of type `A` and return a value of type `T`.
* @param {T | null} [defaultResult=null] The default result to be returned if an error is thrown during the execution of `fnc`. If not provided, the default value is `null`.
*
* @returns {(...args: A) => T | null} A new function that takes the same arguments as `fnc` and returns a value of type `T` or `null`.
* This function will execute `fnc` with the provided arguments, log any error that occurs during execution, and return either the result of `fnc` or the default result.
*
* @example
* const safeDivide = TryAndLogOnError((x: number, y: number) => x / y, Infinity);
* const result = safeDivide(4, 0); // Logs error and returns Infinity
*
*/
function TryAndLogOnError(fnc, defaultResult = null) {
return (...args) => {
let result = defaultResult;
try {
result = fnc(...args);
}
catch (e) {
console.error(e.message, e);
}
return result;
};
}
/**
* `TryAndLogOnErrorAsync` is a higher-order function that wraps a provided function `fnc` with error handling logic.
* It attempts to execute `fnc` and logs any errors that occur during its execution. If an error is thrown, it returns a default result.
*
* @template T The expected return type of the function `fnc`.
* @template A An array-like type representing the arguments of the function `fnc`.
*
* @param {(...args: A) => T | Promise<T>} fnc The function to be executed. This function can be either synchronous or asynchronous.
* It should return a value of type `T` or a Promise that resolves to a value of type `T`.
*
* @param {T | null} [defaultResult=null] The default result to be returned if an error is thrown during the execution of `fnc`.
* If not provided, the default value is `null`.
*
* @returns {(...args: A) => Promise<T | null>} A function that takes the same arguments as `fnc` and returns a Promise.
* The Promise will resolve to the result of `fnc` if it executes successfully, or to `defaultResult` if an error is thrown.
* Any error that occurs during the execution of `fnc` will be logged to the console.
*
* @example
* const safeDivide = TryAndLogOnErrorAsync((x, y) => x / y, 'Error');
* safeDivide(10, 2).then(console.log); // Logs 5
* safeDivide(10, 0).then(console.log); // Logs 'Error' and logs the error to the console
*/
function TryAndLogOnErrorAsync(fnc, defaultResult = null) {
return (...args) => tslib_1.__awaiter(this, void 0, void 0, function* () {
let result = defaultResult;
try {
result = yield fnc(...args);
}
catch (e) {
console.error(e.message, e);
}
return result;
});
}
//# sourceMappingURL=try-log.js.map