aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src
diff options
context:
space:
mode:
authorDavid Cho-Lerat <david.cho-lerat@sonarsource.com>2024-04-02 11:35:53 +0200
committersonartech <sonartech@sonarsource.com>2024-04-02 20:02:42 +0000
commit9b9f82ec3c1470416e5f823d49567b193d3e5945 (patch)
treecebec404faa67caff020ef921c7b1ba67596863e /server/sonar-web/src
parent9646bb639dfb3c01263d223ab41a88f6ede779e5 (diff)
downloadsonarqube-9b9f82ec3c1470416e5f823d49567b193d3e5945.tar.gz
sonarqube-9b9f82ec3c1470416e5f823d49567b193d3e5945.zip
SONAR-21691 Do not retry react-queries that return 4xx error codes
Diffstat (limited to 'server/sonar-web/src')
-rw-r--r--server/sonar-web/src/main/js/app/utils/startReactApp.tsx5
-rw-r--r--server/sonar-web/src/main/js/queries/__tests__/queryClient-test.ts47
-rw-r--r--server/sonar-web/src/main/js/queries/devops-integration.ts1
-rw-r--r--server/sonar-web/src/main/js/queries/queryClient.ts40
4 files changed, 89 insertions, 4 deletions
diff --git a/server/sonar-web/src/main/js/app/utils/startReactApp.tsx b/server/sonar-web/src/main/js/app/utils/startReactApp.tsx
index e7ccd54642f..cb034260408 100644
--- a/server/sonar-web/src/main/js/app/utils/startReactApp.tsx
+++ b/server/sonar-web/src/main/js/app/utils/startReactApp.tsx
@@ -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
index 00000000000..670fe0f0e4c
--- /dev/null
+++ b/server/sonar-web/src/main/js/queries/__tests__/queryClient-test.ts
@@ -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);
+});
diff --git a/server/sonar-web/src/main/js/queries/devops-integration.ts b/server/sonar-web/src/main/js/queries/devops-integration.ts
index 6713052e21d..6c487127bf8 100644
--- a/server/sonar-web/src/main/js/queries/devops-integration.ts
+++ b/server/sonar-web/src/main/js/queries/devops-integration.ts
@@ -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
index 00000000000..b25d32c0222
--- /dev/null
+++ b/server/sonar-web/src/main/js/queries/queryClient.ts
@@ -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;
+ },
+ },
+ },
+});