stormy-gold
stormy-gold9mo ago

api request retries on failure, how to stop

If api fails due to 401, it still retry to fetch
5 Replies
kapa.ai
kapa.ai9mo ago
Hi @viveksingh0143. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The behavior you're experiencing might be due to the retry strategy of the HTTP client you're using. If you're using axios, for example, it doesn't automatically retry failed requests. However, if you're using a library or a custom setup that automatically retries failed requests, that might be the reason why it attempts to fetch multiple times before calling onError. Unfortunately, the provided extracts do not contain information on how to customize the retry strategy directly within refine. You might need to configure this in your HTTP client or the library you're using for API calls. For axios, you can use the axios-retry library to customize the retry strategy. Here's an example:
import axios from 'axios';
import axiosRetry from 'axios-retry';

const axiosInstance = axios.create();

axiosRetry(axiosInstance, { retries: 1 }); // Only retry once before failing
import axios from 'axios';
import axiosRetry from 'axios-retry';

const axiosInstance = axios.create();

axiosRetry(axiosInstance, { retries: 1 }); // Only retry once before failing
In this example, axios will only retry once before failing. You can adjust the retries option to fit your needs. Source (https://discord.com/channels/837692625737613362/1130517760452087919) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@940119979334967366> kapa.ai is still learning and improving, please let us know how it did by reacting below
stormy-gold
stormy-gold9mo ago
@kapa.ai no custom setup to axios, but it tries multiple times when receive 401
kapa.ai
kapa.ai9mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- If you're not using a custom setup or library that automatically retries failed requests, axios should not be retrying on a 401 error. However, if you're using refine, the onError method of the authProvider might be causing this behavior. In refine, the onError method is called when an error response is received from the API. If this method returns a promise resolving to an object with logout: true, the user will be logged out. This might be the reason why your application is attempting to fetch multiple times when a 401 error is received. Here's an example of how the onError method can be set up to log out the user if the API returns a 401 error:
const authProvider: AuthBindings = {
onError: async (error) => {
if (error.status === 401) {
return {
logout: true,
redirectTo: "/login",
error,
};
}
return {};
},
};
const authProvider: AuthBindings = {
onError: async (error) => {
if (error.status === 401) {
return {
logout: true,
redirectTo: "/login",
error,
};
}
return {};
},
};
Source (https://discord.com/channels/837692625737613362/1151401556294504518) If your onError method is set up in a similar way, it might be logging out the user when a 401 error (which usually indicates an expired token) is received. You might want to adjust this logic to not log out the user immediately when a 401 error is received. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@940119979334967366> kapa.ai is still learning and improving, please let us know how it did by reacting below