Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through uninstall error messages from backend #22208

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading