advanced-retry
Version:
A retry library with advanced features
39 lines (38 loc) • 2.26 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.customErrorResolver = void 0;
const base_1 = require("../filter/base");
/**
* @description Custom retry error resolver is used to handle the error and retry the operation to your needs
* @param configuration - Custom retry policy. Configure the initial data, such as how many attempts to make, or what data to pass to the operation
* @param canHandleError - Used to filter the error. If this handler is not able to handle the error, it will be passed to the next handler
* @param callback - The callback function that will be called to handle the error and try to resolve it. Here you can specify custom logic. It can optionally pass context to the next iteration
* @returns Error resolver
*/
const customErrorResolver = ({ configuration, canHandleError, callback, }) => (_a) => __awaiter(void 0, [_a], void 0, function* ({ error, attempt, retryContext: context, abortSignal }) {
if (canHandleError == undefined ||
(0, base_1.toErrorFilter)(canHandleError).canHandleError(error, attempt, context)) {
/* istanbul ignore next */
const r = yield callback(error, attempt, configuration, context.data, abortSignal);
return {
remainingAttempts: r.remainingAttempts,
unrecoverable: r.unrecoverable,
context: { data: r.context },
};
}
return {
remainingAttempts: -1,
unrecoverable: false,
context: { data: undefined },
};
});
exports.customErrorResolver = customErrorResolver;