Skip to content

Commit

Permalink
Handle 404 error responses in queryClient
Browse files Browse the repository at this point in the history
  • Loading branch information
robines committed Dec 17, 2024
1 parent 2d28bf5 commit 9ce0efa
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AuthContextProvider } from '~/context/AuthContext';
import '~/global.scss';
import { QueryCache, QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { AxiosError } from 'axios';
import { t } from 'i18next';
import { RouterProvider } from 'react-router-dom';
import { toast } from 'react-toastify';
Expand All @@ -17,12 +18,25 @@ const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error, query) => {
console.error(error);

// Don't show toast on HTTP 404 errors
if (error instanceof AxiosError && error.response?.status === 404) {
return;
}

toast.error((query.meta?.errorMsg as string) ?? t(KEY.common_something_went_wrong));
},
}),
defaultOptions: {
queries: {
retry: 2, // default is 3
retry: (failureCount, error) => {
// Don't retry on HTTP 404 responses
if (error instanceof AxiosError && error.response?.status === 404) {
return false;
}
// max 2 retries
return failureCount < 2;
},
},
},
});
Expand Down

0 comments on commit 9ce0efa

Please sign in to comment.