File tree Expand file tree Collapse file tree 3 files changed +46
-2
lines changed
frontend/src/pages/hardwareProfiles/manage Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import { HardwareProfileFormData } from '#~/pages/hardwareProfiles/manage/types'
1313import { createHardwareProfile , updateHardwareProfile } from '#~/api' ;
1414import useNotification from '#~/utilities/useNotification' ;
1515import { useDashboardNamespace } from '#~/redux/selectors' ;
16+ import { humanizeHardwareProfileError } from '#~/pages/hardwareProfiles/manage/utils' ;
1617
1718type 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 ) ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 1+ const K8S_RESOURCE_PATTERN = / h a r d w a r e p r o f i l e s \. \S + / gi;
2+
3+ export const humanizeHardwareProfileError = ( message : string ) : string => {
4+ if ( / a l r e a d y e x i s t s / 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+ } ;
You can’t perform that action at this time.
0 commit comments