Common Try-Catch function, applies to Object containing methods/function

/**
 * @description Common Try-Catch function, applies to Object contains methods/function
 * @param {Object.<string,Function|any>} DATA_OBJ Object contains methods/function
 * @param {String} NAME  Name of the Object
 * @returns {void}
 */
const applyTryCatch = function applyTryCatch(DATA_OBJ, NAME) {
    /**
     * @description  Try-Catch function
     * @author Manu Antony
     * @param {Function} myfunction - reference to a function
     * @param {String} key - name of the function
     * @returns {Function|false}
     */
    const tryCatch = function (myfunction, key) {
        return function () {
            try {
                return myfunction.apply(this, arguments);
            } catch (e) {
                log.error("error in " + key, e);
                ERROR_STACK.push(e.toString());
                return false;
            }
        };
    }
    for (let key in DATA_OBJ) {
        if (typeof DATA_OBJ[key] === "function") {
            DATA_OBJ[key] = tryCatch(DATA_OBJ[key], NAME + "." + key);
        }
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *