Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): prevent ApiError [object Object] from FastAPI 422 detail (#13498)

seer-by-sentry[bot]2230a3696939
diff --git a/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts b/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
index 170a375bdb..cd37453607 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/__tests__/custom-mutator.test.ts
@@ -269,3 +269,51 @@ describe("customMutator — empty body handling", () => {
expect(result.data).toEqual({ ok: true });
});
});
+
+describe("customMutator validation errors", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockIsClientSide.mockReturnValue(true);
+ mockGetSystemHeaders.mockReturnValue({});
+ mockGetTraceData.mockReturnValue({});
+ });
+
+ it.each([
+ [
+ {
+ detail: [
+ { msg: "Field required", loc: ["body", "name"] },
+ { msg: "Invalid value" },
+ ],
+ },
+ "Field required; Invalid value",
+ ],
+ [{ detail: "Not allowed" }, "Not allowed"],
+ [{ detail: { reason: "Invalid input" } }, '{"reason":"Invalid input"}'],
+ [
+ { detail: [{ reason: "Invalid input" }, null] },
+ '{"reason":"Invalid input"}; null',
+ ],
+ [{ detail: null, message: "Fallback message" }, "Fallback message"],
+ [{ detail: [], message: "Fallback message" }, "Fallback message"],
+ [{ message: "Missing detail" }, "Missing detail"],
+ ])("formats response %j as a readable message", async (body, message) => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: false,
+ status: 422,
+ statusText: "Unprocessable Entity",
+ headers: new Headers({ "content-type": "application/json" }),
+ json: async () => body,
+ }),
+ );
+
+ await expect(
+ customMutator("/test", { method: "POST" }),
+ ).rejects.toMatchObject({
+ message,
+ status: 422,
+ });
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts b/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
index 5dc9369581..67014e53ac 100644
--- a/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
+++ b/autogpt_platform/frontend/src/app/api/mutators/custom-mutator.ts
@@ -131,8 +131,23 @@ export const customMutator = async <
responseData = { error: "Failed to parse response" };
}
+ const rawDetail = responseData?.detail;
+ const detail = Array.isArray(rawDetail)
+ ? rawDetail
+ .map((e: unknown) =>
+ e && typeof e === "object" && "msg" in e
+ ? String((e as { msg: unknown }).msg)
+ : JSON.stringify(e),
+ )
+ .join("; ")
+ : typeof rawDetail === "string"
+ ? rawDetail
+ : rawDetail != null
+ ? JSON.stringify(rawDetail)
+ : undefined;
+
const errorMessage =
- responseData?.detail ||
+ detail ||
responseData?.message ||
response.statusText ||
`HTTP ${response.status}`;