UNPKG

@radixdlt/application

Version:

A JavaScript client library for interacting with the Radix Distributed Ledger.

31 lines 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.retryOnErrorCode = exports.hasRequiredProps = void 0; const util_1 = require("@radixdlt/util"); const neverthrow_1 = require("neverthrow"); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const hasRequiredProps = (methodName, obj, props) => { for (const prop of props) { if (obj[prop] === undefined) { return (0, neverthrow_1.err)([ Error(`Prop validation failed for ${methodName} response. ${prop} was undefined.`), ]); } } return (0, neverthrow_1.ok)(obj); }; exports.hasRequiredProps = hasRequiredProps; const retryOnErrorCode = ({ maxRetryAttempts = 3, scalingDuration = 1000, errorCodes = [], } = {}) => (attempts) => attempts.pipe((0, operators_1.mergeMap)(({ error }, i) => { const retryAttempt = i + 1; const foundErrorCode = errorCodes.some(e => e === error.code); // if maximum number of retries have been met // or response is a error code we don't wish to retry, throw error if (retryAttempt > maxRetryAttempts || !foundErrorCode) { return (0, rxjs_1.throwError)(() => error); } util_1.log.debug(`Attempt ${retryAttempt}: retrying in ${retryAttempt * scalingDuration}ms`); return (0, rxjs_1.timer)(retryAttempt * scalingDuration); })); exports.retryOnErrorCode = retryOnErrorCode; //# sourceMappingURL=utils.js.map