[WEB-4525] fix: project create/update error handling (#7429)
* chore: project create/update error handling * chore: code refactor
This commit is contained in:
parent
1ad792b4bb
commit
07c80bb02c
2 changed files with 42 additions and 68 deletions
|
|
@ -99,8 +99,11 @@ export const CreateProjectForm: FC<TCreateProjectFormProps> = observer((props) =
|
||||||
// Handle the new error format where codes are nested in arrays under field names
|
// Handle the new error format where codes are nested in arrays under field names
|
||||||
const errorData = err?.data ?? {};
|
const errorData = err?.data ?? {};
|
||||||
|
|
||||||
// Check for specific error codes in the new format
|
const nameError = errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST");
|
||||||
if (errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST")) {
|
const identifierError = errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST");
|
||||||
|
|
||||||
|
if (nameError || identifierError) {
|
||||||
|
if (nameError) {
|
||||||
setToast({
|
setToast({
|
||||||
type: TOAST_TYPE.ERROR,
|
type: TOAST_TYPE.ERROR,
|
||||||
title: t("toast.error"),
|
title: t("toast.error"),
|
||||||
|
|
@ -108,36 +111,20 @@ export const CreateProjectForm: FC<TCreateProjectFormProps> = observer((props) =
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST")) {
|
if (identifierError) {
|
||||||
setToast({
|
setToast({
|
||||||
type: TOAST_TYPE.ERROR,
|
type: TOAST_TYPE.ERROR,
|
||||||
title: t("toast.error"),
|
title: t("toast.error"),
|
||||||
message: t("project_identifier_already_taken"),
|
message: t("project_identifier_already_taken"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Handle other field-specific errors (excluding name and identifier which are handled above)
|
|
||||||
Object.keys(errorData).forEach((field) => {
|
|
||||||
// Skip name and identifier fields as they're handled separately above
|
|
||||||
if (field === "name" || field === "identifier") return;
|
|
||||||
|
|
||||||
const fieldErrors = errorData[field];
|
|
||||||
if (Array.isArray(fieldErrors)) {
|
|
||||||
fieldErrors.forEach((errorMessage) => {
|
|
||||||
setToast({
|
setToast({
|
||||||
type: TOAST_TYPE.ERROR,
|
type: TOAST_TYPE.ERROR,
|
||||||
title: t("error"),
|
title: t("toast.error"),
|
||||||
message: errorMessage,
|
message: t("something_went_wrong"),
|
||||||
});
|
|
||||||
});
|
|
||||||
} else if (typeof fieldErrors === "string") {
|
|
||||||
setToast({
|
|
||||||
type: TOAST_TYPE.ERROR,
|
|
||||||
title: t("error"),
|
|
||||||
message: fieldErrors,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Fallback error handling if the error processing fails
|
// Fallback error handling if the error processing fails
|
||||||
console.error("Error processing API error:", error);
|
console.error("Error processing API error:", error);
|
||||||
|
|
|
||||||
|
|
@ -116,8 +116,11 @@ export const ProjectDetailsForm: FC<IProjectDetailsForm> = (props) => {
|
||||||
// Handle the new error format where codes are nested in arrays under field names
|
// Handle the new error format where codes are nested in arrays under field names
|
||||||
const errorData = err ?? {};
|
const errorData = err ?? {};
|
||||||
|
|
||||||
// Check for specific error codes in the new format
|
const nameError = errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST");
|
||||||
if (errorData.name?.includes("PROJECT_NAME_ALREADY_EXIST")) {
|
const identifierError = errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST");
|
||||||
|
|
||||||
|
if (nameError || identifierError) {
|
||||||
|
if (nameError) {
|
||||||
setToast({
|
setToast({
|
||||||
type: TOAST_TYPE.ERROR,
|
type: TOAST_TYPE.ERROR,
|
||||||
title: t("toast.error"),
|
title: t("toast.error"),
|
||||||
|
|
@ -125,36 +128,20 @@ export const ProjectDetailsForm: FC<IProjectDetailsForm> = (props) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorData?.identifier?.includes("PROJECT_IDENTIFIER_ALREADY_EXIST")) {
|
if (identifierError) {
|
||||||
setToast({
|
setToast({
|
||||||
type: TOAST_TYPE.ERROR,
|
type: TOAST_TYPE.ERROR,
|
||||||
title: t("toast.error"),
|
title: t("toast.error"),
|
||||||
message: t("project_identifier_already_taken"),
|
message: t("project_identifier_already_taken"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Handle other field-specific errors (excluding name and identifier which are handled above)
|
|
||||||
Object.keys(errorData).forEach((field) => {
|
|
||||||
// Skip name and identifier fields as they're handled separately above
|
|
||||||
if (field === "name" || field === "identifier") return;
|
|
||||||
|
|
||||||
const fieldErrors = errorData[field];
|
|
||||||
if (Array.isArray(fieldErrors)) {
|
|
||||||
fieldErrors.forEach((errorMessage) => {
|
|
||||||
setToast({
|
setToast({
|
||||||
type: TOAST_TYPE.ERROR,
|
type: TOAST_TYPE.ERROR,
|
||||||
title: t("error"),
|
title: t("toast.error"),
|
||||||
message: errorMessage,
|
message: t("something_went_wrong"),
|
||||||
});
|
|
||||||
});
|
|
||||||
} else if (typeof fieldErrors === "string") {
|
|
||||||
setToast({
|
|
||||||
type: TOAST_TYPE.ERROR,
|
|
||||||
title: t("error"),
|
|
||||||
message: fieldErrors,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Fallback error handling if the error processing fails
|
// Fallback error handling if the error processing fails
|
||||||
console.error("Error processing API error:", error);
|
console.error("Error processing API error:", error);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue