Skip to content

Commit

Permalink
Update secret error messages across the UI (#25085)
Browse files Browse the repository at this point in the history
relates to #24550

more updates to the various secret error messages after some API changes

- [x] Manual QA for all new/changed functionality
  • Loading branch information
ghernandez345 authored Jan 2, 2025
1 parent f2a15bc commit 631af6b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -60,7 +61,7 @@ export const getErrorMessage = (err: AxiosResponse<IApiError>) => {
}

if (apiReason.includes("Secret variable")) {
return apiReason.replace("missing from database", "doesn't exist");
return generateSecretErrMsg(err);
}

return apiReason || DEFAULT_ERROR_MESSAGE;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.";

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -33,6 +32,7 @@ import { IFleetMaintainedAppFormData } from "./FleetAppDetailsForm/FleetAppDetai
import AddFleetAppSoftwareModal from "./AddFleetAppSoftwareModal";

import {
getErrorMessage,
getFleetAppPolicyDescription,
getFleetAppPolicyName,
getFleetAppPolicyQuery,
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, string> = {
Expand Down Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

Expand Down Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions frontend/pages/SoftwarePage/helpers.ts
Original file line number Diff line number Diff line change
@@ -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.");
};

0 comments on commit 631af6b

Please sign in to comment.