This is an automated email from the ASF dual-hosted git repository.
chihsuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new 32931301b77 HDDS-17691. [Recon][UI]Handle failed GET rejections in
useApiData (#11943)
32931301b77 is described below
commit 32931301b77b9a21a1d3270c2fc3a348fbd3f252
Author: sravani <[email protected]>
AuthorDate: Tue Nov 17 15:31:58 2026 +0530
HDDS-17691. [Recon][UI]Handle failed GET rejections in useApiData (#11943)
---
.../src/__tests__/containers/Containers.test.tsx | 7 +-
.../src/__tests__/heatmap/Heatmap.test.tsx | 16 +----
.../src/__tests__/hooks/useAPIData.hook.test.tsx | 77 ++++++++++++++++++++++
.../src/v2/hooks/useAPIData.hook.ts | 6 +-
4 files changed, 86 insertions(+), 20 deletions(-)
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
index 722591f0862..213e9159405 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
@@ -49,13 +49,12 @@ vi.mock('@/v2/hooks/useAPIData.hook', async
(importOriginal) => {
const actual = await importOriginal<typeof
import('@/v2/hooks/useAPIData.hook')>();
return {
...actual,
- useApiData: <T,>(url: string, defaultValue: T, options = {}) => {
- return actual.useApiData(url, defaultValue, {
+ useApiData: <T,>(url: string, defaultValue: T, options = {}) =>
+ actual.useApiData(url, defaultValue, {
...options,
retryAttempts: 0,
retryDelay: 0,
- });
- },
+ }),
};
});
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/heatmap/Heatmap.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/heatmap/Heatmap.test.tsx
index 6d31d35b964..3d44f067e0f 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/heatmap/Heatmap.test.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/heatmap/Heatmap.test.tsx
@@ -54,22 +54,12 @@ vi.mock('@/v2/hooks/useAPIData.hook', async
(importOriginal) => {
const actual = await importOriginal<typeof
import('@/v2/hooks/useAPIData.hook')>();
return {
...actual,
- useApiData: <T,>(url: string, defaultValue: T, options = {}) => {
- // The hook's own initial GET does not handle a rejected request, so a
404/500 on mount surfaces as an
- // unhandled rejection in tests. Skip it and issue the initial request
with a handled promise instead.
- const api = actual.useApiData(url, defaultValue, {
+ useApiData: <T,>(url: string, defaultValue: T, options = {}) =>
+ actual.useApiData(url, defaultValue, {
...options,
- initialFetch: false,
retryAttempts: 0,
retryDelay: 0,
- });
- React.useEffect(() => {
- if (url) {
- api.execute().catch(() => undefined);
- }
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
- return api;
- },
+ }),
};
});
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/hooks/useAPIData.hook.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/hooks/useAPIData.hook.test.tsx
new file mode 100644
index 00000000000..2c26aa23163
--- /dev/null
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/hooks/useAPIData.hook.test.tsx
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import React from 'react';
+import { render, screen, waitFor } from '@testing-library/react';
+import { http, HttpResponse } from 'msw';
+import { setupServer } from 'msw/node';
+
+import { useApiData } from '@/v2/hooks/useAPIData.hook';
+
+const RETRY_TEST_URL = '/api/v1/use-api-data-retry-test';
+
+let requestCount = 0;
+
+const retryTestServer = setupServer(
+ http.get(RETRY_TEST_URL, () => {
+ requestCount++;
+ return HttpResponse.json({ message: 'Internal Server Error' }, { status:
500 });
+ })
+);
+
+function RetryProbe({ onError }: { onError: (error: unknown) => void }) {
+ const { loading, error } = useApiData(RETRY_TEST_URL, null, {
+ retryAttempts: 2,
+ retryDelay: 25,
+ onError,
+ });
+
+ return (
+ <div>
+ <span data-testid="loading">{String(loading)}</span>
+ <span data-testid="error">{error ?? ''}</span>
+ </div>
+ );
+}
+
+describe('useApiData', () => {
+ describe('retry on 5xx', () => {
+ beforeAll(() => retryTestServer.listen({ onUnhandledRequest: 'error' }));
+ afterEach(() => {
+ retryTestServer.resetHandlers();
+ requestCount = 0;
+ });
+ afterAll(() => retryTestServer.close());
+
+ test('retries until attempts are exhausted and invokes onError', async ()
=> {
+ const onError = vi.fn();
+ render(<RetryProbe onError={onError} />);
+
+ await waitFor(
+ () => {
+ expect(onError).toHaveBeenCalledTimes(1);
+ },
+ { timeout: 3000 }
+ );
+
+ expect(requestCount).toBe(3);
+ expect(screen.getByTestId('loading')).toHaveTextContent('false');
+ expect(screen.getByTestId('error').textContent).not.toBe('');
+ });
+ });
+});
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAPIData.hook.ts
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAPIData.hook.ts
index cc97a599a0f..b9f2259cb13 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAPIData.hook.ts
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAPIData.hook.ts
@@ -142,7 +142,7 @@ export function useApiData<T>(
(!error.response?.status || error.response?.status >= 500)) {
retryCountRef.current++;
retryTimeoutRef.current = setTimeout(() => {
- executeRequest(requestData, true);
+ void executeRequest(requestData, true).catch(() => undefined);
}, retryDelay * retryCountRef.current);
return Promise.reject(error);
}
@@ -198,12 +198,12 @@ export function useApiData<T>(
// by checking if the component is mounted or just the URL has changed.
mountedRef.current = true;
if (initialFetch && method === 'GET') {
- executeRequest();
+ void executeRequest().catch(() => undefined);
}
} else {
// URL changed - refetch for GET requests
if (method === 'GET') {
- executeRequest();
+ void executeRequest().catch(() => undefined);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]