Skip to content

Commit

Permalink
fix: improve error handling in resource policy setting modals
Browse files Browse the repository at this point in the history
  • Loading branch information
nowgnuesLee committed Jan 23, 2025
1 parent 9c5a840 commit a7b2451
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
60 changes: 30 additions & 30 deletions react/src/components/KeypairResourcePolicySettingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,25 @@ const KeypairResourcePolicySettingModal: React.FC<
props: props as CreateKeyPairResourcePolicyInput,
},
onCompleted: (res, errors) => {
if (!res?.create_keypair_resource_policy?.ok || errors) {
if (res.create_keypair_resource_policy?.msg) {
message.error(
painKiller.relieve(res.create_keypair_resource_policy.msg),
);
} else if (errors && errors.length > 0) {
// if res.create_keypair_resource_policy is null
errors.forEach((error) =>
message.error(painKiller.relieve(error.message), 2.5),
);
}
if (
!res?.create_keypair_resource_policy?.ok &&
res.create_keypair_resource_policy?.msg
) {
message.error(res.create_keypair_resource_policy.msg);
onRequestClose(false);
} else {
message.success(t('resourcePolicy.SuccessfullyCreated'));
onRequestClose(true);
return;
}
if (errors && errors.length > 0) {
errors.forEach((error) => message.error(error.message, 2.5));
onRequestClose(false);
return;
}

message.success(t('resourcePolicy.SuccessfullyCreated'));
onRequestClose(true);
},
onError(err) {
message.error(err.message);
message.error(painKiller.relieve(err?.message));
},
});
} else {
Expand All @@ -249,25 +249,25 @@ const KeypairResourcePolicySettingModal: React.FC<
props: props as ModifyKeyPairResourcePolicyInput,
},
onCompleted: (res, errors) => {
if (!res?.modify_keypair_resource_policy?.ok || errors) {
if (res.modify_keypair_resource_policy?.msg) {
message.error(
painKiller.relieve(res.modify_keypair_resource_policy.msg),
);
} else if (errors && errors.length > 0) {
// if res.create_keypair_resource_policy is null
errors.forEach((error) =>
message.error(painKiller.relieve(error.message), 2.5),
);
}
if (
!res?.modify_keypair_resource_policy?.ok &&
res.modify_keypair_resource_policy?.msg
) {
message.error(res.modify_keypair_resource_policy.msg);
onRequestClose(false);
} else {
message.success(t('resourcePolicy.SuccessfullyCreated'));
onRequestClose(true);
return;
}
if (errors && errors.length > 0) {
errors.forEach((error) => message.error(error.message, 2.5));
onRequestClose(false);
return;
}

message.success(t('resourcePolicy.SuccessfullyUpdated'));
onRequestClose(true);
},
onError(err) {
message.error(err.message);
message.error(painKiller.relieve(err?.message));
},
});
}
Expand Down
18 changes: 14 additions & 4 deletions react/src/components/ProjectResourcePolicySettingModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GBToBytes, bytesToGB } from '../helper';
import { SIGNED_32BIT_MAX_INT } from '../helper/const-vars';
import { useSuspendedBackendaiClient } from '../hooks';
import { usePainKiller } from '../hooks/usePainKiller';
import BAIModal, { BAIModalProps } from './BAIModal';
import Flex from './Flex';
import FormItemWithUnlimited from './FormItemWithUnlimited';
Expand Down Expand Up @@ -43,6 +44,7 @@ const ProjectResourcePolicySettingModal: React.FC<Props> = ({
const { t } = useTranslation();
const { token } = theme.useToken();
const { message } = App.useApp();
const painKiller = usePainKiller();
const formRef = useRef<FormInstance>(null);

const baiClient = useSuspendedBackendaiClient();
Expand Down Expand Up @@ -157,7 +159,11 @@ const ProjectResourcePolicySettingModal: React.FC<Props> = ({
},
onCompleted(res, errors) {
if (!res?.create_project_resource_policy?.ok || errors) {
message.error(res?.create_project_resource_policy?.msg);
message.error(
painKiller.relieve(
res?.create_project_resource_policy?.msg as string,
),
);
onRequestClose();
} else {
message.success(
Expand All @@ -167,7 +173,7 @@ const ProjectResourcePolicySettingModal: React.FC<Props> = ({
}
},
onError(error) {
message.error(error?.message);
message.error(painKiller.relieve(error?.message));
},
});
} else {
Expand All @@ -178,7 +184,11 @@ const ProjectResourcePolicySettingModal: React.FC<Props> = ({
},
onCompleted(res, errors) {
if (!res?.modify_project_resource_policy?.ok || errors) {
message.error(res?.modify_project_resource_policy?.msg);
message.error(
painKiller.relieve(
res?.modify_project_resource_policy?.msg as string,
),
);
onRequestClose();
} else {
message.success(
Expand All @@ -188,7 +198,7 @@ const ProjectResourcePolicySettingModal: React.FC<Props> = ({
}
},
onError(error) {
message.error(error?.message);
message.error(painKiller.relieve(error?.message));
},
});
}
Expand Down
19 changes: 15 additions & 4 deletions react/src/components/UserResourcePolicySettingModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GBToBytes, bytesToGB } from '../helper';
import { SIGNED_32BIT_MAX_INT } from '../helper/const-vars';
import { useSuspendedBackendaiClient } from '../hooks';
import { usePainKiller } from '../hooks/usePainKiller';
import BAIModal, { BAIModalProps } from './BAIModal';
import Flex from './Flex';
import FormItemWithUnlimited from './FormItemWithUnlimited';
Expand Down Expand Up @@ -43,6 +44,8 @@ const UserResourcePolicySettingModal: React.FC<Props> = ({
const { t } = useTranslation();
const { token } = theme.useToken();
const { message } = App.useApp();
const painKiller = usePainKiller();

const formRef = useRef<FormInstance>(null);

const baiClient = useSuspendedBackendaiClient();
Expand Down Expand Up @@ -166,7 +169,11 @@ const UserResourcePolicySettingModal: React.FC<Props> = ({
},
onCompleted(res, errors) {
if (!res?.create_user_resource_policy?.ok || errors) {
message.error(res?.create_user_resource_policy?.msg);
message.error(
painKiller.relieve(
res?.create_user_resource_policy?.msg as string,
),
);
onRequestClose();
} else {
message.success(
Expand All @@ -176,7 +183,7 @@ const UserResourcePolicySettingModal: React.FC<Props> = ({
}
},
onError(error) {
message.error(error?.message);
message.error(painKiller.relieve(error?.message));
},
});
} else {
Expand All @@ -187,7 +194,11 @@ const UserResourcePolicySettingModal: React.FC<Props> = ({
},
onCompleted(res, errors) {
if (!res?.modify_user_resource_policy?.ok || errors) {
message.error(res?.modify_user_resource_policy?.msg);
message.error(
painKiller.relieve(
res?.modify_user_resource_policy?.msg as string,
),
);
onRequestClose();
} else {
message.success(
Expand All @@ -197,7 +208,7 @@ const UserResourcePolicySettingModal: React.FC<Props> = ({
}
},
onError(error) {
message.error(error?.message);
message.error(painKiller.relieve(error?.message));
},
});
}
Expand Down

0 comments on commit a7b2451

Please sign in to comment.