workadventure/front/src/Connexion/AxiosUtils.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

import axios from "axios";
import * as rax from "retry-axios";
2021-12-04 18:30:12 +01:00
import { errorStore } from "../Stores/ErrorStore";
/**
* 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,
statusCodesToRetry: [
[100, 199],
[429, 429],
[501, 599],
],
// 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) => {
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}'`);
errorStore.addErrorMessage("Unable to connect to WorkAdventure. Are you connected to internet?", {
closable: false,
2021-12-04 18:30:12 +01:00
id: "axios_retry",
});
},
};
2021-12-04 18:30:12 +01:00
axiosWithRetry.interceptors.response.use((res) => {
if (res.status < 400) {
errorStore.clearMessageById("axios_retry");
}
return res;
});
const interceptorId = rax.attach(axiosWithRetry);