diff --git a/frontend/pages/ManageControlsPage/OSSettings/cards/CustomSettings/components/ProfileUploader/helpers.tsx b/frontend/pages/ManageControlsPage/OSSettings/cards/CustomSettings/components/ProfileUploader/helpers.tsx index f33d5fc08f10..4a3defbf1cf7 100644 --- a/frontend/pages/ManageControlsPage/OSSettings/cards/CustomSettings/components/ProfileUploader/helpers.tsx +++ b/frontend/pages/ManageControlsPage/OSSettings/cards/CustomSettings/components/ProfileUploader/helpers.tsx @@ -1,6 +1,7 @@ import React from "react"; import { AxiosResponse } from "axios"; import { IApiError } from "interfaces/errors"; +import { generateSecretErrMsg } from "pages/SoftwarePage/helpers"; export const parseFile = async (file: File): Promise<[string, string]> => { // get the file name and extension @@ -60,7 +61,7 @@ export const getErrorMessage = (err: AxiosResponse) => { } if (apiReason.includes("Secret variable")) { - return apiReason.replace("missing from database", "doesn't exist"); + return generateSecretErrMsg(err); } return apiReason || DEFAULT_ERROR_MESSAGE; diff --git a/frontend/pages/ManageControlsPage/Scripts/components/ScriptUploader/helpers.ts b/frontend/pages/ManageControlsPage/Scripts/components/ScriptUploader/helpers.ts index f341ec38b953..016efb8b6e08 100644 --- a/frontend/pages/ManageControlsPage/Scripts/components/ScriptUploader/helpers.ts +++ b/frontend/pages/ManageControlsPage/Scripts/components/ScriptUploader/helpers.ts @@ -1,4 +1,5 @@ import { getErrorReason } from "interfaces/errors"; +import { generateSecretErrMsg } from "pages/SoftwarePage/helpers"; const DEFAULT_ERROR_MESSAGE = "Couldn't upload. Please try again."; @@ -13,7 +14,7 @@ export const getErrorMessage = (err: unknown) => { ) { return "Couldn't upload. The file should be .sh or .ps1 file."; } else if (apiErrMessage.includes("Secret variable")) { - return apiErrMessage.replace("missing from database", "doesn't exist"); + return generateSecretErrMsg(err); } return apiErrMessage || DEFAULT_ERROR_MESSAGE; diff --git a/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareCustomPackage/helpers.tsx b/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareCustomPackage/helpers.tsx index 99a09e229bcc..06e3c2e23fdc 100644 --- a/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareCustomPackage/helpers.tsx +++ b/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareCustomPackage/helpers.tsx @@ -6,6 +6,8 @@ import { LEARN_MORE_ABOUT_BASE_LINK } from "utilities/constants"; import CustomLink from "components/CustomLink"; +import { generateSecretErrMsg } from "pages/SoftwarePage/helpers"; + const DEFAULT_ERROR_MESSAGE = "Couldn't add. Please try again."; // eslint-disable-next-line import/prefer-default-export @@ -30,7 +32,7 @@ export const getErrorMessage = (err: unknown) => { ); } else if (reason.includes("Secret variable")) { - return reason.replace("missing from database", "doesn't exist"); + return generateSecretErrMsg(err); } else if (reason.includes("Unable to extract necessary metadata")) { return ( <> diff --git a/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/FleetMaintainedAppDetailsPage.tsx b/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/FleetMaintainedAppDetailsPage.tsx index d47ccd9ef045..8516ff248378 100644 --- a/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/FleetMaintainedAppDetailsPage.tsx +++ b/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/FleetMaintainedAppDetailsPage.tsx @@ -12,7 +12,6 @@ import labelsAPI, { getCustomLabels } from "services/entities/labels"; import { QueryContext } from "context/query"; import { AppContext } from "context/app"; import { NotificationContext } from "context/notification"; -import { getErrorReason } from "interfaces/errors"; import { Platform, PLATFORM_DISPLAY_NAMES } from "interfaces/platform"; import { ILabelSummary } from "interfaces/label"; import useToggleSidePanel from "hooks/useToggleSidePanel"; @@ -33,6 +32,7 @@ import { IFleetMaintainedAppFormData } from "./FleetAppDetailsForm/FleetAppDetai import AddFleetAppSoftwareModal from "./AddFleetAppSoftwareModal"; import { + getErrorMessage, getFleetAppPolicyDescription, getFleetAppPolicyName, getFleetAppPolicyQuery, @@ -192,7 +192,7 @@ const FleetMaintainedAppDetailsPage = ({ } catch (error) { // quick exit if there was an error adding the software. Skip the policy // creation. - renderFlash("error", getErrorReason(error)); + renderFlash("error", getErrorMessage(error)); setShowAddFleetAppSoftwareModal(false); return; } diff --git a/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/helpers.tsx b/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/helpers.tsx index 3ccdf8df4c3c..3a1f93411e6d 100644 --- a/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/helpers.tsx +++ b/frontend/pages/SoftwarePage/SoftwareAddPage/SoftwareFleetMaintained/FleetMaintainedAppDetailsPage/helpers.tsx @@ -1,3 +1,7 @@ +import { getErrorReason } from "interfaces/errors"; + +import { generateSecretErrMsg } from "pages/SoftwarePage/helpers"; + import fleetAppData from "../../../../../../server/mdm/maintainedapps/apps.json"; const NameToIdentifierMap: Record = { @@ -40,3 +44,13 @@ export const getFleetAppPolicyDescription = (appName: string) => { export const getFleetAppPolicyQuery = (name: string) => { return getFleetAppData(name)?.automatic_policy_query; }; + +export const getErrorMessage = (err: unknown) => { + const reason = getErrorReason(err); + + if (reason.includes("Secret variable")) { + return generateSecretErrMsg(err); + } + + return reason; +}; diff --git a/frontend/pages/SoftwarePage/SoftwareTitleDetailsPage/EditSoftwareModal/helpers.tsx b/frontend/pages/SoftwarePage/SoftwareTitleDetailsPage/EditSoftwareModal/helpers.tsx index 9b4151003cfe..df399a8c5c34 100644 --- a/frontend/pages/SoftwarePage/SoftwareTitleDetailsPage/EditSoftwareModal/helpers.tsx +++ b/frontend/pages/SoftwarePage/SoftwareTitleDetailsPage/EditSoftwareModal/helpers.tsx @@ -6,6 +6,7 @@ import { ISoftwarePackage } from "interfaces/software"; import CustomLink from "components/CustomLink"; import { LEARN_MORE_ABOUT_BASE_LINK } from "utilities/constants"; +import { generateSecretErrMsg } from "pages/SoftwarePage/helpers"; const DEFAULT_ERROR_MESSAGE = "Couldn't edit software. Please try again."; @@ -36,9 +37,7 @@ export const getErrorMessage = (err: unknown, software: ISoftwarePackage) => { ); } else if (reason.includes("Secret variable")) { - return reason - .replace("missing from database", "doesn't exist") - .replace("Couldn't add", "Couldn't edit"); + return generateSecretErrMsg(err).replace("Couldn't add", "Couldn't edit"); } return reason || DEFAULT_ERROR_MESSAGE; diff --git a/frontend/pages/SoftwarePage/helpers.ts b/frontend/pages/SoftwarePage/helpers.ts new file mode 100644 index 000000000000..48901f0efc52 --- /dev/null +++ b/frontend/pages/SoftwarePage/helpers.ts @@ -0,0 +1,40 @@ +import { getErrorReason } from "interfaces/errors"; + +/** + * helper function to generate error message for secret variables based + * on the error reason. + */ +// eslint-disable-next-line import/prefer-default-export +export const generateSecretErrMsg = (err: unknown) => { + const reason = getErrorReason(err); + + let errorType = ""; + if (getErrorReason(err, { nameEquals: "install script" })) { + errorType = "install script"; + } else if (getErrorReason(err, { nameEquals: "post-install script" })) { + errorType = "post-install script"; + } else if (getErrorReason(err, { nameEquals: "uninstall script" })) { + errorType = "uninstall script"; + } else if (getErrorReason(err, { nameEquals: "profile" })) { + errorType = "profile"; + } + + if (errorType === "profile") { + return reason + .split(":")[1] + .replace(/Secret variables?/i, "Variable") + .replace("missing from database", "doesn't exist."); + } + + // all other specific error types + if (errorType) { + return reason + .replace(/Secret variables?/i, `Variable used in ${errorType} `) + .replace("missing from database", "doesn't exist."); + } + + // no spcial error type. return generic secret error message + return reason + .replace(/Secret variables?/i, "Variable") + .replace("missing from database", "doesn't exist."); +};