Skip to content

Commit

Permalink
update the label error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ghernandez345 committed Dec 19, 2024
1 parent e6efcf7 commit cc5e0db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
10 changes: 2 additions & 8 deletions frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import {
MANAGE_HOSTS_PAGE_FILTER_KEYS,
MANAGE_HOSTS_PAGE_LABEL_INCOMPATIBLE_QUERY_PARAMS,
} from "./HostsPageConfig";
import { isAcceptableStatus } from "./helpers";
import { getDeleteLabelErrorMessages, isAcceptableStatus } from "./helpers";

import DeleteSecretModal from "../../../components/EnrollSecrets/DeleteSecretModal";
import SecretEditorModal from "../../../components/EnrollSecrets/SecretEditorModal";
Expand Down Expand Up @@ -1061,13 +1061,7 @@ const ManageHostsPage = ({
);
renderFlash("success", "Successfully deleted label.");
} catch (error) {
console.error(error);
renderFlash(
"error",
getErrorReason(error).includes("built-in")
? "Built-in labels can’t be modified or deleted."
: "Could not delete label. Please try again."
);
renderFlash("error", getDeleteLabelErrorMessages(error));
} finally {
setIsUpdatingLabel(false);
}
Expand Down
24 changes: 24 additions & 0 deletions frontend/pages/hosts/ManageHostsPage/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getErrorReason } from "interfaces/errors";

export const isAcceptableStatus = (filter: string): boolean => {
return (
filter === "new" ||
Expand All @@ -21,3 +23,25 @@ export const isValidPemCertificate = (cert: string): boolean => {

return regexPemHeader.test(cert) && regexPemFooter.test(cert);
};

function hasStatusKey(value: unknown): value is { status: number } {
return (
typeof value === "object" &&
value !== null &&
"status" in value &&
typeof (value as any).status === "number"
);
}

export const getDeleteLabelErrorMessages = (error: unknown): string => {
// unprocessable content status. Label is used in a custom profile
// or software target. we have to check that status exists on the error object
// before we can access it.
if (hasStatusKey(error) && error.status === 422) {
return getErrorReason(error).includes("built-in")
? "Built-in labels can't be modified or deleted."
: "Couldn't delete. Software uses this label as a custom target. Please delete the software and try again.";
}

return "Could not delete label. Please try again.";
};

0 comments on commit cc5e0db

Please sign in to comment.