Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(automations): show the reason when a draft save is refused (#18462)

Engel Nyste145534802e3
diff --git a/__tests__/components/automations/automation-setup-panel.test.tsx b/__tests__/components/automations/automation-setup-panel.test.tsx
index 05d8d49efe..cb1fa14385 100644
--- a/__tests__/components/automations/automation-setup-panel.test.tsx
+++ b/__tests__/components/automations/automation-setup-panel.test.tsx
@@ -1857,6 +1857,42 @@ describe("AutomationSetupPanel", () => {
);
});
+ it("shows the API's reason and drops Ready to test when a draft save is refused", async () => {
+ const user = userEvent.setup();
+ renderPanel();
+ mockSavedDraftEcho();
+ await saveDraftAndWaitForTest(user);
+ expect(
+ screen.getByTestId("automation-setup-draft-validity"),
+ ).toHaveTextContent("AUTOMATION_SETUP$READY_TO_TEST");
+
+ vi.mocked(AutomationService.updateServerDraft).mockRejectedValue(
+ new HttpError(422, "Unprocessable Entity", {
+ detail: {
+ message: "Draft body does not match endpoint schema",
+ errors: [
+ {
+ field: "timeout",
+ code: "value_error",
+ message:
+ "Value error, timeout must not exceed 1800 seconds (30 minutes)",
+ },
+ ],
+ },
+ }),
+ );
+ await user.click(screen.getByTestId("automation-setup-save-draft"));
+
+ await waitFor(() =>
+ expect(displayErrorToast).toHaveBeenCalledWith(
+ "Value error, timeout must not exceed 1800 seconds (30 minutes)",
+ ),
+ );
+ expect(
+ screen.queryByTestId("automation-setup-draft-validity"),
+ ).not.toBeInTheDocument();
+ });
+
it("requires valid JSON before testing an event draft", async () => {
const user = userEvent.setup();
renderPanel();
diff --git a/__tests__/utils/api-error-message.test.ts b/__tests__/utils/api-error-message.test.ts
index 35262a97f6..bd3b77fcb6 100644
--- a/__tests__/utils/api-error-message.test.ts
+++ b/__tests__/utils/api-error-message.test.ts
@@ -36,6 +36,55 @@ describe("getApiErrorMessage", () => {
);
});
+ it("prefers the structured `detail.errors` over the generic `detail.message`", () => {
+ const error = new HttpError(422, "Unprocessable Entity", {
+ detail: {
+ message: "Draft body does not match endpoint schema",
+ errors: [
+ {
+ field: "timeout",
+ code: "value_error",
+ message:
+ "Value error, timeout must not exceed 1800 seconds (30 minutes)",
+ },
+ ],
+ },
+ });
+
+ expect(getApiErrorMessage(error, "fallback")).toBe(
+ "Value error, timeout must not exceed 1800 seconds (30 minutes)",
+ );
+ });
+
+ it("prefixes each structured `detail.errors` message with its field when there are several", () => {
+ const error = new HttpError(422, "Unprocessable Entity", {
+ detail: {
+ message: "Draft body does not match endpoint schema",
+ errors: [
+ { field: "timeout", message: "Too long" },
+ { field: "trigger.schedule", message: "Invalid cron" },
+ ],
+ },
+ });
+
+ expect(getApiErrorMessage(error, "fallback")).toBe(
+ "timeout: Too long; trigger.schedule: Invalid cron",
+ );
+ });
+
+ it("falls back to `detail.message` when `detail.errors` is empty", () => {
+ const error = new HttpError(422, "Unprocessable Entity", {
+ detail: {
+ message: "Draft body does not match endpoint schema",
+ errors: [],
+ },
+ });
+
+ expect(getApiErrorMessage(error, "fallback")).toBe(
+ "Draft body does not match endpoint schema",
+ );
+ });
+
it("returns the reason from an Agent Server unhandled-error body, not its generic `detail`", () => {
const error = new HttpError(500, "Internal Server Error", {
detail: "Internal Server Error",
diff --git a/src/components/features/automations/setup/automation-setup-panel.tsx b/src/components/features/automations/setup/automation-setup-panel.tsx
index 7898ecb0af..7915f45dcb 100644
--- a/src/components/features/automations/setup/automation-setup-panel.tsx
+++ b/src/components/features/automations/setup/automation-setup-panel.tsx
@@ -428,6 +428,7 @@ function DraftStatusBar({
runs,
isSubmitting,
canTest,
+ saveFailed,
onTest,
onViewRuns,
}: {
@@ -435,6 +436,8 @@ function DraftStatusBar({
runs: AutomationRun[];
isSubmitting: boolean;
canTest: boolean;
+ /** The latest save failed, so `draft` no longer describes the form. */
+ saveFailed: boolean;
onTest: () => void;
onViewRuns: () => void;
}) {
@@ -468,7 +471,7 @@ function DraftStatusBar({
</span>
</div>
<div className="flex shrink-0 items-center gap-1.5">
- {validationMessage ? null : (
+ {validationMessage || saveFailed ? null : (
<span
data-testid="automation-setup-draft-validity"
className={validityClassName}
@@ -1887,6 +1890,7 @@ export function AutomationSetupPanel({
runs={draftRuns}
isSubmitting={isSubmitting}
canTest={isEditingExisting || canTestDraft}
+ saveFailed={saveState === "error"}
onTest={() => {
setIsViewingDraftRuns(true);
void (isEditingExisting
diff --git a/src/utils/api-error-message.ts b/src/utils/api-error-message.ts
index 428febccdb..8357bc5220 100644
--- a/src/utils/api-error-message.ts
+++ b/src/utils/api-error-message.ts
@@ -49,6 +49,37 @@ function getValidationDetailMessage(detail: unknown): string | null {
.join("; ");
}
+/**
+ * Join the messages of a structured `detail.errors` array
+ * (`[{ field, message }, ...]`, as the automation draft API sends next to a
+ * generic `detail.message`), or return null when there are none. With
+ * several errors each message is prefixed with its field.
+ */
+function getStructuredDetailErrorsMessage(detail: unknown): string | null {
+ if (!detail || typeof detail !== "object" || Array.isArray(detail)) {
+ return null;
+ }
+ const { errors } = detail as { errors?: unknown };
+ if (!Array.isArray(errors)) return null;
+ const entries = errors
+ .filter(
+ (item): item is { field?: unknown; message: string } =>
+ !!item &&
+ typeof item === "object" &&
+ typeof (item as { message?: unknown }).message === "string" &&
+ (item as { message: string }).message !== "",
+ )
+ .map(({ field, message }) => ({
+ field: typeof field === "string" && field ? field : null,
+ message,
+ }));
+ if (entries.length === 0) return null;
+ if (entries.length === 1) return entries[0].message;
+ return entries
+ .map(({ field, message }) => (field ? `${field}: ${message}` : message))
+ .join("; ");
+}
+
/**
* The fixed `detail` the Agent Server sends with every 5xx (an unhandled
* error, or an `HTTPException` with a 5xx status); the reason is under
@@ -59,7 +90,8 @@ const AGENT_SERVER_GENERIC_DETAIL = "Internal Server Error";
/**
* Extract a human-readable message from a failed API call. Prefers the
* server-provided `message`/`detail`/`exception`/`error` fields (including a
- * FastAPI validation `detail` array and a structured `detail.message`), then
+ * FastAPI validation `detail` array and a structured `detail.errors` or
+ * `detail.message`, the specific errors winning over the summary), then
* the `Error` message, then `fallback`. `exception` is used only when `detail`
* is missing, empty or the Agent Server's generic "Internal Server Error", so
* a specific `detail` always wins over the technical `exception` text. The
@@ -92,6 +124,7 @@ export function getApiErrorMessage(error: unknown, fallback: string): string {
typeof detail === "string" && detail
? detail
: validationMessage ||
+ getStructuredDetailErrorsMessage(detail) ||
(typeof structuredDetailMessage === "string" &&
structuredDetailMessage
? structuredDetailMessage