2021-11-29 18:15:21 +01:00
|
|
|
import axios from "axios";
|
|
|
|
import * as rax from "retry-axios";
|
2021-12-04 18:30:12 +01:00
|
|
|
import { errorStore } from "../Stores/ErrorStore";
|
2022-01-19 11:36:08 +01:00
|
|
|
import { _ } from "../Translator/Translator";
|
2021-11-29 18:15:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This instance of Axios will retry in case of an issue and display an error message as a HTML overlay.
|
|
|
|
*/
|
|
|
|
export const axiosWithRetry = axios.create();
|
|
|
|
axiosWithRetry.defaults.raxConfig = {
|
|
|
|
instance: axiosWithRetry,
|
|
|
|
retry: Infinity,
|
|
|
|
noResponseRetries: Infinity,
|
|
|
|
|
|
|
|
maxRetryAfter: 60_000,
|
|
|
|
|
2021-12-08 10:58:53 +01:00
|
|
|
statusCodesToRetry: [
|
|
|
|
[100, 199],
|
|
|
|
[429, 429],
|
|
|
|
[501, 599],
|
|
|
|
],
|
|
|
|
|
2021-11-29 18:15:21 +01:00
|
|
|
// You can detect when a retry is happening, and figure out how many
|
|
|
|
// retry attempts have been made
|
2021-12-04 18:30:12 +01:00
|
|
|
onRetryAttempt: (err) => {
|
2021-11-29 18:15:21 +01:00
|
|
|
const cfg = rax.getConfig(err);
|
2021-12-04 18:30:12 +01:00
|
|
|
console.log(err);
|
|
|
|
console.log(cfg);
|
|
|
|
console.log(`Retry attempt #${cfg?.currentRetryAttempt} on URL '${err.config.url}'`);
|
2022-01-19 11:36:08 +01:00
|
|
|
errorStore.addErrorMessage(
|
|
|
|
_("error.connection-retry.unable-to-connect-to-workAdventure-are-you-connected-to-internet"),
|
|
|
|
{
|
|
|
|
closable: false,
|
|
|
|
id: "axios_retry",
|
|
|
|
}
|
|
|
|
);
|
2021-11-29 18:15:21 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-12-04 18:30:12 +01:00
|
|
|
axiosWithRetry.interceptors.response.use((res) => {
|
2021-11-29 18:15:21 +01:00
|
|
|
if (res.status < 400) {
|
|
|
|
errorStore.clearMessageById("axios_retry");
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
|
|
|
|
const interceptorId = rax.attach(axiosWithRetry);
|