Skip to content

Commit

Permalink
Pass through uninstall error messages from backend (#22208)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
iansltx committed Sep 18, 2024
1 parent 0b5b129 commit 210e404
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
6 changes: 3 additions & 3 deletions frontend/pages/hosts/details/cards/Software/HostSoftware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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();
Expand All @@ -211,7 +211,7 @@ const HostSoftware = ({
</>
);
} catch (e) {
renderFlash("error", "Couldn't uninstall. Please try again.");
renderFlash("error", getUninstallErrorMessage(e));
}
setSoftwareIdActionPending(null);
refetchSoftware();
Expand Down
32 changes: 29 additions & 3 deletions frontend/pages/hosts/details/cards/Software/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")}.`;
Expand All @@ -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")) {
Expand All @@ -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;
};

0 comments on commit 210e404

Please sign in to comment.