我要重试http请求,我正在使用axios http请求库。
我正在尝试使用retry-axios库(https://github.com/JustinBeckwith/retry-axios),但是在catch块中出现以下错误
无法读取未定义的属性“ attach”
import axios from "axios";
import rax from "retry-axios";
try {
...
const interceptorId = rax.attach();
const metrics = await axios(this.getMetrics(session._id, sessionRequest._id));
} catch(err) {
console.log(err);
}
public getMetrics(sessionId: any, requestId: any) {
const url = this.config.backendUrl + "/check/metrics";
const options = {
url,
method: "get",
headers: {
"X-IDCHECK-SESSION-ID": sessionId,
},
data: {},
raxConfig: {
// Retry 3 times on requests that return a response (500, etc) before giving up. Defaults to 3.
retry: 3,
// Retry twice on errors that don't return a response (ENOTFOUND, ETIMEDOUT, etc).
noResponseRetries: 2,
// Milliseconds to delay at first. Defaults to 100.
retryDelay: 100,
// HTTP methods to automatically retry. Defaults to:
// ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT']
httpMethodsToRetry: ["GET", "HEAD", "OPTIONS", "DELETE", "PUT"],
// The response status codes to retry. Supports a double
// array with a list of ranges. Defaults to:
// [[100, 199], [429, 429], [500, 599]]
statusCodesToRetry: [[100, 199], [429, 429], [500, 599]],
// If you are using a non static instance of Axios you need
// to pass that instance here (const ax = axios.create())
// instance: ax,
// You can detect when a retry is happening, and figure out how many
// retry attempts have been made
onRetryAttempt: (err) => {
const cfg = rax.getConfig(err);
console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
},
},
};
return options;
}