Skip to content

Commit

Permalink
feat(namadillo): avoid retrying for 4xx errors (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrorezende authored Aug 15, 2024
1 parent 73cd206 commit f67437c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
50 changes: 50 additions & 0 deletions apps/namadillo/src/App/Common/QueryProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { queryClientAtom } from "jotai-tanstack-query";
import { Provider as JotaiProvider } from "jotai/react";
import { useHydrateAtoms } from "jotai/utils";

type ErrorWithResponse = Error & {
response?: {
status?: number;
};
};

const MAX_RETRIES = 3;

export const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: (failureCount, error: ErrorWithResponse) => {
if (failureCount > MAX_RETRIES) {
return false;
}

// Handles all 4xx errors
if (String(error?.response?.status).startsWith("4")) {
return false;
}

return true;
},
},
},
});

const HydrateAtoms = (props: { children: JSX.Element }): JSX.Element => {
useHydrateAtoms([[queryClientAtom, queryClient]]);
return props.children;
};

export const QueryProvider = ({
children,
}: {
children: JSX.Element;
}): JSX.Element => {
return (
<QueryClientProvider client={queryClient}>
<JotaiProvider>
<HydrateAtoms>{children}</HydrateAtoms>
</JotaiProvider>
</QueryClientProvider>
);
};
6 changes: 3 additions & 3 deletions apps/namadillo/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { init as initShared } from "@namada/shared/src/init-inline";
import { AppSetup } from "App/AppSetup";
import { StoreProvider } from "atoms/store";
import { QueryProvider } from "App/Common/QueryProvider";
import { SdkProvider } from "hooks/useSdk";
import React from "react";
import { createRoot } from "react-dom/client";
Expand All @@ -18,15 +18,15 @@ if (container) {
initShared().then(() => {
root.render(
<React.StrictMode>
<StoreProvider>
<QueryProvider>
<IntegrationsProvider>
<AppSetup>
<SdkProvider>
<RouterProvider router={getRouter()} />
</SdkProvider>
</AppSetup>
</IntegrationsProvider>
</StoreProvider>
</QueryProvider>
</React.StrictMode>
);
});
Expand Down

0 comments on commit f67437c

Please sign in to comment.