]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21691 Do not retry react-queries that return 4xx error codes
authorDavid Cho-Lerat <david.cho-lerat@sonarsource.com>
Tue, 2 Apr 2024 09:35:53 +0000 (11:35 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 2 Apr 2024 20:02:42 +0000 (20:02 +0000)
server/sonar-web/src/main/js/app/utils/startReactApp.tsx
server/sonar-web/src/main/js/queries/__tests__/queryClient-test.ts [new file with mode: 0644]
server/sonar-web/src/main/js/queries/devops-integration.ts
server/sonar-web/src/main/js/queries/queryClient.ts [new file with mode: 0644]

index e7ccd54642f4ab079503a9ba2f12bec3f3b30d7a..cb034260408a643d52382812774954c647c001a8 100644 (file)
@@ -19,7 +19,7 @@
  */
 
 import { ThemeProvider } from '@emotion/react';
-import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { QueryClientProvider } from '@tanstack/react-query';
 import { ToastMessageContainer, lightTheme } from 'design-system';
 import * as React from 'react';
 import { createRoot } from 'react-dom/client';
@@ -70,6 +70,7 @@ import webAPIRoutes from '../../apps/web-api/routes';
 import webhooksRoutes from '../../apps/webhooks/routes';
 import { translate } from '../../helpers/l10n';
 import { getBaseUrl } from '../../helpers/system';
+import { queryClient } from '../../queries/queryClient';
 import { AppState } from '../../types/appstate';
 import { Feature } from '../../types/features';
 import { CurrentUser } from '../../types/users';
@@ -252,8 +253,6 @@ const router = createBrowserRouter(
   { basename: getBaseUrl() },
 );
 
-const queryClient = new QueryClient();
-
 export default function startReactApp(
   l10nBundle: IntlShape,
   currentUser?: CurrentUser,
diff --git a/server/sonar-web/src/main/js/queries/__tests__/queryClient-test.ts b/server/sonar-web/src/main/js/queries/__tests__/queryClient-test.ts
new file mode 100644 (file)
index 0000000..670fe0f
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+import { QueryClient } from '@tanstack/react-query';
+import { queryClient } from '../queryClient';
+
+jest.mock('@tanstack/react-query');
+
+it('should return the queryClient and not retry on 4xx errors', () => {
+  expect(queryClient).toBeDefined();
+
+  expect(jest.mocked(QueryClient)).toHaveBeenCalledWith({
+    defaultOptions: { queries: { retry: expect.any(Function) } },
+  });
+
+  const retryFunction = jest.mocked(QueryClient).mock.calls[0][0]?.defaultOptions?.queries
+    ?.retry as Function;
+
+  expect(retryFunction(0, undefined)).toEqual(true);
+  expect(retryFunction(1, undefined)).toEqual(true);
+  expect(retryFunction(2, undefined)).toEqual(false);
+
+  expect(retryFunction(0, null)).toEqual(true);
+  expect(retryFunction(0, {})).toEqual(true);
+  expect(retryFunction(0, { status: 200 })).toEqual(true);
+
+  expect(retryFunction(0, { status: 400 })).toEqual(false);
+  expect(retryFunction(0, { status: 404 })).toEqual(false);
+  expect(retryFunction(0, { status: 500 })).toEqual(true);
+});
index 6713052e21d694df835072d16e673e1baece34dc..6c487127bf8e781d75688d73e5233338469275e2 100644 (file)
@@ -68,7 +68,6 @@ export function useProjectBindingQuery<T = ProjectAlmBindingResponse>(
         return e;
       }),
     staleTime: 60_000,
-    retry: false,
     enabled: projectKey !== null,
     ...options,
   });
diff --git a/server/sonar-web/src/main/js/queries/queryClient.ts b/server/sonar-web/src/main/js/queries/queryClient.ts
new file mode 100644 (file)
index 0000000..b25d32c
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+import { QueryClient } from '@tanstack/react-query';
+
+export const queryClient = new QueryClient({
+  defaultOptions: {
+    queries: {
+      retry: (failureCount, error) => {
+        if (typeof error === 'object' && error !== null && 'status' in error) {
+          const { status } = error as unknown as Response;
+
+          if (status >= 400 && status < 500) {
+            // no point in retrying on 4xx errors
+            return false;
+          }
+        }
+
+        return failureCount < 2;
+      },
+    },
+  },
+});