diff --git a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx index 341caa4e7f..7c50898133 100644 --- a/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx +++ b/frontend/pages/hosts/ManageHostsPage/ManageHostsPage.tsx @@ -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"; @@ -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); } diff --git a/frontend/pages/hosts/ManageHostsPage/helpers.ts b/frontend/pages/hosts/ManageHostsPage/helpers.ts index ca78607641..5a30ea4405 100644 --- a/frontend/pages/hosts/ManageHostsPage/helpers.ts +++ b/frontend/pages/hosts/ManageHostsPage/helpers.ts @@ -1,3 +1,5 @@ +import { getErrorReason } from "interfaces/errors"; + export const isAcceptableStatus = (filter: string): boolean => { return ( filter === "new" || @@ -21,3 +23,25 @@ export const isValidPemCertificate = (cert: string): boolean => { return regexPemHeader.test(cert) && regexPemFooter.test(cert); }; + +const 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."; +};