'use strict';

module.exports = connector => handlers => ({
  js: (event, context, cb) => {
    const promisesArray = handlers[`${event.handler}`];
    if (!promisesArray) {
      const handlerName = context.functionName.split('-').pop().split("'")[0];
      const error = new Error(`[404] Event-handler '${event.handler}' not found in function-handler '${handlerName}'`);
      return cb(error);
    }
    try {
      return connector.apply(null, promisesArray)(event, context, cb);
    } catch (err) {
      return cb(err);
    }
  }
});
