Skip to content

Commit 14f4db3

Browse files
committed
when fetching groups, capture error 403 as rbac failure and cache result
1 parent 672b772 commit 14f4db3

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

frontend/src/pages/projects/projectSharing/useGroups.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@ import * as React from 'react';
22
import useFetchState, { FetchState } from '~/utilities/useFetchState';
33
import { listGroups } from '~/api';
44
import { GroupKind } from '~/k8sTypes';
5-
import { useUser } from '~/redux/selectors';
65

76
const useGroups = (): FetchState<GroupKind[]> => {
8-
const { isAdmin } = useUser();
7+
const is403 = React.useRef(false);
98

10-
const getGroup = React.useCallback(() => {
11-
if (!isAdmin) {
12-
return Promise.resolve([]);
13-
}
14-
return listGroups().catch((e) => {
15-
if (e.statusObject?.code === 404) {
16-
throw new Error('No groups found.');
17-
}
18-
throw e;
19-
});
20-
}, [isAdmin]);
9+
const getGroup = React.useCallback(
10+
async () =>
11+
is403.current
12+
? []
13+
: listGroups().catch((e) => {
14+
if (e.statusObject?.code === 403) {
15+
is403.current = true;
16+
return [];
17+
}
18+
if (e.statusObject?.code === 404) {
19+
throw new Error('No groups found.');
20+
}
21+
throw e;
22+
}),
23+
[],
24+
);
2125

2226
return useFetchState<GroupKind[]>(getGroup, []);
2327
};

0 commit comments

Comments
 (0)