Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(automations): show the API's message when an automation edit fails (#18391)

Engel Nyst821197e9d7f5
diff --git a/__tests__/components/automations/automation-setup-panel.test.tsx b/__tests__/components/automations/automation-setup-panel.test.tsx
index 6ef72e3cf4..05d8d49efe 100644
--- a/__tests__/components/automations/automation-setup-panel.test.tsx
+++ b/__tests__/components/automations/automation-setup-panel.test.tsx
@@ -10,6 +10,7 @@ import userEvent from "@testing-library/user-event";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { HttpError } from "@openhands/typescript-client";
+import { AxiosError } from "axios";
import {
NavigationProvider,
type NavigationContextValue,
@@ -199,6 +200,13 @@ vi.mock("#/manifests/automation-interface", () => ({
: "/v1/preset/prompt",
}));
+/** An axios error carrying a server response, as the Local transport throws. */
+function axiosResponseError(status: number, data: unknown): AxiosError {
+ const error = new AxiosError(`Request failed with status code ${status}`);
+ error.response = { status, data } as AxiosError["response"];
+ return error;
+}
+
function renderPanel(
draft: AutomationSetupDraft = {
prompt: "Review every pull request",
@@ -1993,6 +2001,95 @@ describe("AutomationSetupPanel", () => {
expect(mockNavigate).toHaveBeenCalledWith("/automations");
});
+ describe("existing automation request failures", () => {
+ const editDraft: AutomationSetupDraft = {
+ prompt: "Review every pull request",
+ kind: "prompt",
+ editingAutomationId: "auto-1",
+ form: {
+ kind: "prompt",
+ name: "Daily digest",
+ prompt: "Review every pull request",
+ },
+ };
+
+ it.each([
+ {
+ action: "Save",
+ button: "automation-setup-save",
+ service: "updateAutomation",
+ error: axiosResponseError(422, {
+ detail: [
+ {
+ loc: ["body", "trigger", "schedule"],
+ msg: "Value error, Invalid cron expression: not a cron",
+ type: "value_error",
+ },
+ ],
+ }),
+ expected: "Value error, Invalid cron expression: not a cron",
+ },
+ {
+ action: "Test",
+ button: "automation-setup-test",
+ service: "dispatchAutomation",
+ error: axiosResponseError(404, { detail: "Automation not found" }),
+ expected: "Automation not found",
+ },
+ {
+ action: "Save (Cloud)",
+ button: "automation-setup-save",
+ service: "updateAutomation",
+ error: new HttpError(
+ 404,
+ "Not Found",
+ { detail: "Automation not found" },
+ `HTTP request failed (404 Not Found): {"detail":"Automation not found"}`,
+ ),
+ expected: "Automation not found",
+ },
+ ] as const)(
+ "toasts the API's message when $action fails",
+ async ({ button, service, error, expected }) => {
+ vi.mocked(AutomationService[service]).mockRejectedValue(error);
+ const user = userEvent.setup();
+ renderPanel(editDraft);
+
+ await user.click(screen.getByTestId(button));
+
+ await waitFor(() =>
+ expect(displayErrorToast).toHaveBeenCalledWith(expected),
+ );
+ expect(displayErrorToast).toHaveBeenCalledTimes(1);
+ },
+ );
+ });
+
+ it("toasts the API's message when creating an automation fails", async () => {
+ vi.mocked(AutomationService.createAutomationDraft).mockRejectedValue(
+ axiosResponseError(422, {
+ detail: [
+ {
+ loc: ["body", "timeout"],
+ msg: "Input should be less than or equal to 1800",
+ type: "less_than_equal",
+ },
+ ],
+ }),
+ );
+ const user = userEvent.setup();
+ renderPanel();
+
+ await user.click(screen.getByTestId("automation-setup-create"));
+
+ await waitFor(() =>
+ expect(displayErrorToast).toHaveBeenCalledWith(
+ "Input should be less than or equal to 1800",
+ ),
+ );
+ expect(mockNavigate).not.toHaveBeenCalled();
+ });
+
it("preserves multi-event trigger keys when saving an existing automation", async () => {
vi.mocked(AutomationService.updateAutomation).mockResolvedValue({
id: "auto-1",
diff --git a/src/components/features/automations/setup/automation-setup-panel.tsx b/src/components/features/automations/setup/automation-setup-panel.tsx
index 2c13729955..7898ecb0af 100644
--- a/src/components/features/automations/setup/automation-setup-panel.tsx
+++ b/src/components/features/automations/setup/automation-setup-panel.tsx
@@ -82,6 +82,7 @@ import {
patchConversationInCache,
} from "#/hooks/mutation/conversation-mutation-utils";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
+import { getApiErrorMessage } from "#/utils/api-error-message";
import { useNavigation } from "#/context/navigation-context";
import type {
AutomationDraftApiResponse,
@@ -793,7 +794,7 @@ export function AutomationSetupPanel({
}
updateConversationTags((tags) => buildAutomationSetupModeTags(tags)).catch(
(error: unknown) => {
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(getApiErrorMessage(error, t(I18nKey.ERROR$GENERIC)));
},
);
}, [conversationId, conversationTags, updateConversationTags]);
@@ -846,7 +847,9 @@ export function AutomationSetupPanel({
})
.catch((error: unknown) => {
if (!cancelled) {
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(
+ getApiErrorMessage(error, t(I18nKey.ERROR$GENERIC)),
+ );
}
});
return () => {
@@ -880,7 +883,7 @@ export function AutomationSetupPanel({
setIsTaggedDraftMissing(true);
return;
}
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(getApiErrorMessage(error, t(I18nKey.ERROR$GENERIC)));
})
.finally(() => {
if (!cancelled) setIsHydratingServerDraft(false);
@@ -1371,7 +1374,7 @@ export function AutomationSetupPanel({
return true;
}
setSaveState("error");
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(getApiErrorMessage(error, t(I18nKey.ERROR$GENERIC)));
return false;
}
};
@@ -1505,7 +1508,9 @@ export function AutomationSetupPanel({
return;
}
setSaveState("error");
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(
+ getApiErrorMessage(error, t(I18nKey.AUTOMATION_SETUP$SAVE_FAILED)),
+ );
} finally {
setIsSubmitting(false);
}
@@ -1591,7 +1596,9 @@ export function AutomationSetupPanel({
text: dispatchError,
});
} else {
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(
+ getApiErrorMessage(error, t(I18nKey.AUTOMATIONS$RUN_NOW_ERROR)),
+ );
}
} finally {
setIsSubmitting(false);
@@ -1635,7 +1642,9 @@ export function AutomationSetupPanel({
if (typeof created.id === "string")
navigate(automationDetailPath(created.id));
} catch (error) {
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(
+ getApiErrorMessage(error, t(I18nKey.SETUP$SUBMIT_FAILED)),
+ );
} finally {
setIsSubmitting(false);
}
@@ -1709,7 +1718,9 @@ export function AutomationSetupPanel({
toast.success(t(I18nKey.AUTOMATIONS$EDIT_SUCCESS));
} catch (error) {
setSaveState("error");
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(
+ getApiErrorMessage(error, t(I18nKey.AUTOMATIONS$EDIT_ERROR)),
+ );
} finally {
setIsSubmitting(false);
}
@@ -1729,7 +1740,9 @@ export function AutomationSetupPanel({
text: t(I18nKey.AUTOMATION_SETUP$TEST_DISPATCHED),
});
} catch (error) {
- displayErrorToast(error instanceof Error ? error.message : null);
+ displayErrorToast(
+ getApiErrorMessage(error, t(I18nKey.AUTOMATIONS$RUN_NOW_ERROR)),
+ );
} finally {
setIsSubmitting(false);
}