UNPKG

@bothive/helpers

Version:

Collection of helper functions mainly used inside bothive-core project

45 lines 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const lodash_get_1 = (0, tslib_1.__importDefault)(require("lodash.get")); const config_1 = require("../config"); const timeout_helpers_1 = (0, tslib_1.__importDefault)(require("./timeout.helpers")); function checkForMissingRequiredKeys({ payload, keys, message, meta }) { keys.forEach((key) => { if ((0, lodash_get_1.default)(payload, key, null) == null) { throw new config_1.errorConfig.InvalidObjectError({ message: message && message.replace("{{key}}", key), meta, }); } }); } function retryHandler({ callback, limit = 5, timeout = 1000, onError, }) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { try { const result = yield callback(); return result; } catch (error) { if (onError) { onError(error); } if (!limit) { throw error; } yield timeout_helpers_1.default.wait(timeout); const result = yield retryHandler({ onError, callback, limit: limit - 1, timeout: timeout * 2, }); return result; } }); } exports.default = { checkForMissingRequiredKeys, retryHandler, }; //# sourceMappingURL=error.helpers.js.map