From 210e4047a6b20698faa5f86b5fc798ae618ef303 Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Wed, 18 Sep 2024 11:22:22 -0500 Subject: [PATCH] Pass through uninstall error messages from backend (#22208) # Checklist for submitter - [x] Manual QA for all new/changed functionality [See Slack](https://fleetdm.slack.com/archives/C01EZVBHFHU/p1726605318985819?thread_ts=1726588126.583269&cid=C01EZVBHFHU) from @noahtalerman for context --- .../details/cards/Software/HostSoftware.tsx | 6 ++-- .../hosts/details/cards/Software/helpers.tsx | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/frontend/pages/hosts/details/cards/Software/HostSoftware.tsx b/frontend/pages/hosts/details/cards/Software/HostSoftware.tsx index 1c56843986f8..818fbcd7d989 100644 --- a/frontend/pages/hosts/details/cards/Software/HostSoftware.tsx +++ b/frontend/pages/hosts/details/cards/Software/HostSoftware.tsx @@ -24,7 +24,7 @@ import Spinner from "components/Spinner"; import { generateSoftwareTableHeaders as generateHostSoftwareTableConfig } from "./HostSoftwareTableConfig"; import { generateSoftwareTableHeaders as generateDeviceSoftwareTableConfig } from "./DeviceSoftwareTableConfig"; import HostSoftwareTable from "./HostSoftwareTable"; -import { getErrorMessage } from "./helpers"; +import { getInstallErrorMessage, getUninstallErrorMessage } from "./helpers"; const baseClass = "software-card"; @@ -190,7 +190,7 @@ const HostSoftware = ({ "Software is installing or will install when the host comes online." ); } catch (e) { - renderFlash("error", getErrorMessage(e)); + renderFlash("error", getInstallErrorMessage(e)); } setSoftwareIdActionPending(null); refetchSoftware(); @@ -211,7 +211,7 @@ const HostSoftware = ({ ); } catch (e) { - renderFlash("error", "Couldn't uninstall. Please try again."); + renderFlash("error", getUninstallErrorMessage(e)); } setSoftwareIdActionPending(null); refetchSoftware(); diff --git a/frontend/pages/hosts/details/cards/Software/helpers.tsx b/frontend/pages/hosts/details/cards/Software/helpers.tsx index 2020a1487090..2c12ca9e101d 100644 --- a/frontend/pages/hosts/details/cards/Software/helpers.tsx +++ b/frontend/pages/hosts/details/cards/Software/helpers.tsx @@ -3,7 +3,10 @@ import { getErrorReason } from "interfaces/errors"; import { trimEnd, upperFirst } from "lodash"; const INSTALL_SOFTWARE_ERROR_PREFIX = "Couldn't install."; -const DEFAULT_ERROR_MESSAGE = `${INSTALL_SOFTWARE_ERROR_PREFIX} Please try again.`; +const DEFAULT_INSTALL_ERROR_MESSAGE = `${INSTALL_SOFTWARE_ERROR_PREFIX} Please try again.`; + +const UNINSTALL_SOFTWARE_ERROR_PREFIX = "Couldn't uninstall."; +const DEFAULT_UNINSTALL_ERROR_MESSAGE = `${UNINSTALL_SOFTWARE_ERROR_PREFIX} Please try again.`; const createOnlyInstallableOnMacOSMessage = (reason: string) => `Couldn't install. ${reason.replace("darwin", "macOS")}.`; @@ -28,7 +31,7 @@ const showAPIMessage = (message: string) => { }; // eslint-disable-next-line import/prefer-default-export -export const getErrorMessage = (e: unknown) => { +export const getInstallErrorMessage = (e: unknown) => { const reason = upperFirst(trimEnd(getErrorReason(e), ".")); if (reason.includes("fleetd installed")) { @@ -41,5 +44,28 @@ export const getErrorMessage = (e: unknown) => { return reason; } - return DEFAULT_ERROR_MESSAGE; + return DEFAULT_INSTALL_ERROR_MESSAGE; +}; + +// eslint-disable-next-line import/prefer-default-export +export const getUninstallErrorMessage = (e: unknown) => { + const reason = upperFirst(trimEnd(getErrorReason(e), ".")); + + if ( + reason.includes("run script") || + reason.includes("running script") || + reason.includes("have fleetd") || + reason.includes("only on") + ) { + return `${UNINSTALL_SOFTWARE_ERROR_PREFIX} ${reason}.`; + } else if (reason.startsWith("Couldn't uninstall software.")) { + return reason.replace( + "Couldn't uninstall software.", + "Couldn't uninstall." + ); + } else if (reason.startsWith("No uninstall script exists")) { + return `${UNINSTALL_SOFTWARE_ERROR_PREFIX}. An uninstall script does not exist for this package.`; + } + + return DEFAULT_UNINSTALL_ERROR_MESSAGE; };