should it retry for 404 responses? #372
-
By default when is this a normally behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Yes it is. If you wish, you can use the https://github.com/tannerlinsley/react-query#retries |
Beta Was this translation helpful? Give feedback.
-
When 404 is an acceptable result for a particular call, is there is way to suppress the error log? |
Beta Was this translation helpful? Give feedback.
-
I also believe the library should NOT retry by default on 4XX. Per the HTTP spec 4XX errors are client errors that indicate that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing). The MDN website also has a clear indication that queries should NOT be repeated in that case. @raulingg I would be happy to open a PR if you indicate here that it would be mergeable (in principle, and of course subject to all other requirements around tests, styling... etc) |
Beta Was this translation helpful? Give feedback.
-
For anyone interested in a little snippet. Example with import {QueryClient} from '@tanstack/react-query';
import {isAxiosError} from 'axios';
const MAX_RETRIES = 6;
const HTTP_STATUS_TO_NOT_RETRY = [400, 401, 403, 404];
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: (failureCount, error) => {
if (failureCount > MAX_RETRIES) {
return false;
}
if (
isAxiosError(error) &&
HTTP_STATUS_TO_NOT_RETRY.includes(error.response?.status ?? 0)
) {
console.log(`Aborting retry due to ${error.response?.status} status`);
return false;
}
return true;
},
},
},
}); |
Beta Was this translation helpful? Give feedback.
-
For people using fetch import {QueryClient} from '@tanstack/react-query';
const MAX_RETRIES = 6;
const HTTP_STATUS_TO_NOT_RETRY = [400, 401, 403, 404];
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: (failureCount, error) => {
if (failureCount > MAX_RETRIES) {
return false;
}
if (
Object.hasOwnProperty.call(error, "status") &&
//@ts-ignore
HTTP_STATUS_TO_NOT_RETRY.includes(error.status)
) {
console.log(`Aborting retry due to ${error.status} status`);
return false;
}
return true;
},
},
},
}); |
Beta Was this translation helpful? Give feedback.
Yes it is. If you wish, you can use the
retry
option with a function to determine whether the error should result in a retry:https://github.com/tannerlinsley/react-query#retries