UNPKG

@ts-common/azure-js-dev-tools

Version:

Developer dependencies for TypeScript related projects

103 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.retry = exports.DontRetryError = void 0; var tslib_1 = require("tslib"); /** * An error that can be wrapped around another error that indicates that the action should not be * retried. */ var DontRetryError = /** @class */ (function (_super) { tslib_1.__extends(DontRetryError, _super); function DontRetryError(innerError) { var _this = _super.call(this, "Don't Retry: " + innerError) || this; _this.innerError = innerError; return _this; } return DontRetryError; }(Error)); exports.DontRetryError = DontRetryError; /** * Attempt to run the provided action. If the action throws an error, then it will be retried up to * 2 more times. If the action still throws an error on the 3rd attempt (2nd retry), then the error * will be thrown to the caller of this function. * @param action The action that will be run. */ function retry(action) { return tslib_1.__awaiter(this, void 0, void 0, function () { var maxAttempts, shouldRetry, betweenAttempts, result, attempt, errors, control, _a, error_1; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: maxAttempts = 3; shouldRetry = function (error) { return !!error; }; betweenAttempts = function () { }; if (action && typeof action !== "function") { if (action.maxAttempts != undefined) { maxAttempts = action.maxAttempts; } if (action.shouldRetry) { shouldRetry = action.shouldRetry; } if (action.betweenAttempts) { betweenAttempts = action.betweenAttempts; } action = action.action; } attempt = 0; errors = []; _b.label = 1; case 1: if (!true) return [3 /*break*/, 20]; ++attempt; control = { attempt: attempt, maxAttempts: maxAttempts, errors: errors }; _b.label = 2; case 2: _b.trys.push([2, 12, , 19]); return [4 /*yield*/, Promise.resolve(action(control))]; case 3: result = _b.sent(); if (!(control.shouldRetry === false)) return [3 /*break*/, 4]; return [3 /*break*/, 20]; case 4: _a = control.shouldRetry === true; if (_a) return [3 /*break*/, 6]; return [4 /*yield*/, Promise.resolve(shouldRetry(undefined, result, control))]; case 5: _a = (_b.sent()); _b.label = 6; case 6: if (!_a) return [3 /*break*/, 10]; if (!(attempt >= maxAttempts)) return [3 /*break*/, 7]; throw new Error("Failing retriable action due to no more remaining attempts."); case 7: return [4 /*yield*/, Promise.resolve(betweenAttempts(undefined, result, control))]; case 8: _b.sent(); _b.label = 9; case 9: return [3 /*break*/, 11]; case 10: return [3 /*break*/, 20]; case 11: return [3 /*break*/, 19]; case 12: error_1 = _b.sent(); if (!(error_1 instanceof DontRetryError)) return [3 /*break*/, 13]; throw error_1.innerError; case 13: if (!(control.shouldRetry === false || attempt >= maxAttempts)) return [3 /*break*/, 14]; throw error_1; case 14: return [4 /*yield*/, Promise.resolve(shouldRetry(error_1, undefined, control))]; case 15: if (!!(_b.sent())) return [3 /*break*/, 16]; throw error_1; case 16: return [4 /*yield*/, Promise.resolve(betweenAttempts(error_1, undefined, control))]; case 17: _b.sent(); errors.push(error_1); _b.label = 18; case 18: return [3 /*break*/, 19]; case 19: return [3 /*break*/, 1]; case 20: return [2 /*return*/, result]; } }); }); } exports.retry = retry; //# sourceMappingURL=retry.js.map