Browse Source

SONAR-21691 Do not retry react-queries that return 4xx error codes

tags/10.5.0.89998
David Cho-Lerat 1 month ago
parent
commit
9b9f82ec3c

+ 2
- 3
server/sonar-web/src/main/js/app/utils/startReactApp.tsx View 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,

+ 47
- 0
server/sonar-web/src/main/js/queries/__tests__/queryClient-test.ts View File

@@ -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);
});

+ 0
- 1
server/sonar-web/src/main/js/queries/devops-integration.ts View File

@@ -68,7 +68,6 @@ export function useProjectBindingQuery<T = ProjectAlmBindingResponse>(
return e;
}),
staleTime: 60_000,
retry: false,
enabled: projectKey !== null,
...options,
});

+ 40
- 0
server/sonar-web/src/main/js/queries/queryClient.ts View File

@@ -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;
},
},
},
});

Loading…
Cancel
Save