GladysProject/Gladys

View on GitHub
server/utils/functionsWrapper.js

Summary

Maintainability
A
0 mins
Test Coverage
const logger = require('./logger');

/**
 * @description Wrap a function used to receive an event in a try catch with logging.
 * @param {Function} func - The function to wrap.
 * @returns {Function} Return wrap function.
 * @example
 * eventFunctionWrapper(this.add);
 */
function eventFunctionWrapper(func) {
  return async (...args) => {
    try {
      await func(...args);
    } catch (error) {
      logger.warn(`Error while executing ${func}`);
      logger.warn(error);
    }
  };
}

module.exports = {
  eventFunctionWrapper,
};