Skip to content

Commit e618381

Browse files
fix(RHOAIENG-21136): humanize K8s error messages in hardware profiles (opendatahub-io#6842)
- Add humanizeHardwareProfileError utility that translates raw K8s resource group references (e.g. hardwareprofiles.infrastructure.opendatahub.io) into user-friendly "hardware profile" language - Special-case "already exists" errors to produce a clear duplicate-name message - Apply translation in both create and update error handlers - Add unit tests for all translation branches Fixes: https://redhat.atlassian.net/browse/RHOAIENG-21136 Made-with: Cursor
1 parent c537f2a commit e618381

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

frontend/src/pages/hardwareProfiles/manage/ManageHardwareProfileFooter.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { HardwareProfileFormData } from '#~/pages/hardwareProfiles/manage/types'
1313
import { createHardwareProfile, updateHardwareProfile } from '#~/api';
1414
import useNotification from '#~/utilities/useNotification';
1515
import { useDashboardNamespace } from '#~/redux/selectors';
16+
import { humanizeHardwareProfileError } from '#~/pages/hardwareProfiles/manage/utils';
1617

1718
type ManageHardwareProfileFooterProps = {
1819
state: HardwareProfileFormData;
@@ -60,7 +61,7 @@ const ManageHardwareProfileFooter: React.FC<ManageHardwareProfileFooterProps> =
6061
navigate(redirectPath);
6162
})
6263
.catch((err) => {
63-
setErrorMessage(err.message);
64+
setErrorMessage(humanizeHardwareProfileError(err.message));
6465
})
6566
.finally(() => {
6667
setIsLoading(false);
@@ -84,7 +85,7 @@ const ManageHardwareProfileFooter: React.FC<ManageHardwareProfileFooterProps> =
8485
.then(() => Promise.all(getUpdatePromises(false)))
8586
.then(() => navigate(redirectPath))
8687
.catch((err) => {
87-
setErrorMessage(err.message);
88+
setErrorMessage(humanizeHardwareProfileError(err.message));
8889
})
8990
.finally(() => {
9091
setIsLoading(false);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { humanizeHardwareProfileError } from '#~/pages/hardwareProfiles/manage/utils';
2+
3+
describe('humanizeHardwareProfileError', () => {
4+
it('should return a friendly message when a duplicate name error includes a quoted name', () => {
5+
const k8sError = 'hardwareprofiles.infrastructure.opendatahub.io "my-profile" already exists';
6+
expect(humanizeHardwareProfileError(k8sError)).toBe(
7+
'A hardware profile with the name "my-profile" already exists. Please use a different name.',
8+
);
9+
});
10+
11+
it('should return a friendly message when a duplicate name error has no quoted name', () => {
12+
const k8sError = 'the resource already exists';
13+
expect(humanizeHardwareProfileError(k8sError)).toBe(
14+
'A hardware profile with this name already exists. Please use a different name.',
15+
);
16+
});
17+
18+
it('should replace K8s resource group references in non-duplicate errors', () => {
19+
const k8sError =
20+
'hardwareprofiles.infrastructure.opendatahub.io is forbidden: User cannot create';
21+
expect(humanizeHardwareProfileError(k8sError)).toBe(
22+
'hardware profile is forbidden: User cannot create',
23+
);
24+
});
25+
26+
it('should pass through unrelated error messages unchanged', () => {
27+
const genericError = 'Network error: connection refused';
28+
expect(humanizeHardwareProfileError(genericError)).toBe(genericError);
29+
});
30+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const K8S_RESOURCE_PATTERN = /hardwareprofiles\.\S+/gi;
2+
3+
export const humanizeHardwareProfileError = (message: string): string => {
4+
if (/already exists/i.test(message)) {
5+
const nameMatch = message.match(/"([^"]+)"/);
6+
const name = nameMatch?.[1];
7+
return name
8+
? `A hardware profile with the name "${name}" already exists. Please use a different name.`
9+
: 'A hardware profile with this name already exists. Please use a different name.';
10+
}
11+
12+
return message.replace(K8S_RESOURCE_PATTERN, 'hardware profile');
13+
};

0 commit comments

Comments
 (0)