Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(errors): show the server's message instead of raw HttpError text in generic error toasts (#18051)

Engel Nyst7cd6496040dd
diff --git a/__tests__/query-client-config.behavior.test.ts b/__tests__/query-client-config.behavior.test.ts
index c1eab7b109..4a4941c6ea 100644
--- a/__tests__/query-client-config.behavior.test.ts
+++ b/__tests__/query-client-config.behavior.test.ts
@@ -1,6 +1,9 @@
import { QueryClient } from "@tanstack/react-query";
import { AxiosError } from "axios";
+import { HttpError } from "@openhands/typescript-client";
import { afterEach, describe, expect, it, vi } from "vitest";
+import i18n from "#/i18n";
+import { I18nKey } from "#/i18n/declaration";
import { createAgentServerQueryClient } from "#/query-client-config";
import { __resetActiveStoreForTests } from "#/api/backend-registry/active-store";
import type { Backend } from "#/api/backend-registry/types";
@@ -452,6 +455,62 @@ describe("query client behavior", () => {
expect(toast).toHaveBeenCalledWith(expect.any(String));
});
+
+ // The shared TypeScript client throws an HttpError whose message is the raw
+ // `HTTP request failed (status): {json}` text; the body is on `response`.
+ it.each([
+ {
+ path: "query",
+ execute: executeFailingQuery,
+ detail: "Shared conversation not found",
+ },
+ {
+ path: "mutation",
+ execute: executeFailingMutation,
+ detail: "MCP server 'qa_stale' was not found",
+ },
+ ])(
+ "toasts the server's detail for an SDK HttpError from a failed $path",
+ async ({ execute, detail }) => {
+ // Arrange
+ const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
+ const client = createAgentServerQueryClient();
+ const error = new HttpError(
+ 404,
+ "Not Found",
+ { detail },
+ `HTTP request failed (404 Not Found): ${JSON.stringify({ detail })}`,
+ );
+
+ // Act
+ await expect(execute(client, error)).rejects.toBe(error);
+
+ // Assert
+ expect(toast).toHaveBeenCalledExactlyOnceWith(detail);
+ },
+ );
+
+ it("toasts the translated generic error, not the raw text, for an SDK HttpError without a usable body", async () => {
+ // Arrange β€” the local ingress answers a stopped Agent Server with a
+ // plain-text 502.
+ const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
+ const client = createAgentServerQueryClient();
+ const body = "Bad Gateway: connect ECONNREFUSED 127.0.0.1:8001";
+ const error = new HttpError(
+ 502,
+ "Bad Gateway",
+ body,
+ `HTTP request failed (502 Bad Gateway): ${JSON.stringify(body)}`,
+ );
+
+ // Act
+ await expect(executeFailingMutation(client, error)).rejects.toBe(error);
+
+ // Assert
+ expect(toast).toHaveBeenCalledExactlyOnceWith(
+ i18n.t(I18nKey.ERROR$GENERIC),
+ );
+ });
});
describe("query client selection and proxy behavior", () => {
diff --git a/__tests__/utils/retrieve-axios-error-message.test.ts b/__tests__/utils/retrieve-axios-error-message.test.ts
new file mode 100644
index 0000000000..7317061582
--- /dev/null
+++ b/__tests__/utils/retrieve-axios-error-message.test.ts
@@ -0,0 +1,140 @@
+import { describe, expect, it } from "vitest";
+import { AxiosError, AxiosHeaders } from "axios";
+import { HttpError } from "@openhands/typescript-client";
+import { retrieveAxiosErrorMessage } from "#/utils/retrieve-axios-error-message";
+import {
+ BACKEND_REQUEST_TIMEOUT_MESSAGE,
+ CORS_OR_NETWORK_ERROR_MESSAGE,
+} from "#/utils/user-facing-error";
+
+// The shared TypeScript client throws an HttpError whose message is the raw
+// transport text and whose parsed body is on `response`.
+function createHttpError(status: number, statusText: string, body: unknown) {
+ return new HttpError(
+ status,
+ statusText,
+ body,
+ `HTTP request failed (${status} ${statusText}): ${JSON.stringify(body)}`,
+ );
+}
+
+function createAxiosErrorWithBody(body: unknown) {
+ const error = new AxiosError("Request failed with status code 400");
+ error.response = {
+ data: body,
+ status: 400,
+ statusText: "Bad Request",
+ headers: {},
+ config: { headers: new AxiosHeaders() },
+ };
+ return error;
+}
+
+describe("retrieveAxiosErrorMessage", () => {
+ it.each([
+ {
+ description: "a string `detail`",
+ error: createHttpError(422, "Unprocessable Entity", {
+ detail: "Settings validation failed",
+ }),
+ expected: "Settings validation failed",
+ },
+ {
+ description: "a FastAPI validation `detail` array",
+ error: createHttpError(422, "Unprocessable Entity", {
+ detail: [
+ {
+ type: "string_too_long",
+ loc: ["body", "display_name"],
+ msg: "String should have at most 128 characters",
+ },
+ ],
+ }),
+ expected: "String should have at most 128 characters",
+ },
+ {
+ description: "an `error` field",
+ error: createHttpError(400, "Bad Request", {
+ error: "Repository is not connected",
+ }),
+ expected: "Repository is not connected",
+ },
+ ])(
+ "returns the server's message from an SDK HttpError with $description",
+ ({ error, expected }) => {
+ expect(retrieveAxiosErrorMessage(error)).toBe(expected);
+ },
+ );
+
+ it.each([
+ {
+ description: "an object without a message field",
+ body: { unexpected: true },
+ },
+ {
+ description: "a plain-text gateway body",
+ body: "Bad Gateway: connect ECONNREFUSED 127.0.0.1:8001",
+ },
+ { description: "no body", body: null },
+ ])(
+ "returns an empty string, never the raw transport text, for an SDK HttpError with $description",
+ ({ body }) => {
+ // Callers then show their own localized fallback.
+ expect(
+ retrieveAxiosErrorMessage(createHttpError(502, "Bad Gateway", body)),
+ ).toBe("");
+ },
+ );
+
+ it.each([
+ {
+ description: "an axios error's `error` field",
+ error: createAxiosErrorWithBody({ error: "Axios error field" }),
+ expected: "Axios error field",
+ },
+ {
+ description: "an axios error's `message` field",
+ error: createAxiosErrorWithBody({ message: "Axios message field" }),
+ expected: "Axios message field",
+ },
+ {
+ description: "an axios error's own message when the body has neither",
+ error: createAxiosErrorWithBody({ unexpected: true }),
+ expected: "Request failed with status code 400",
+ },
+ {
+ description: "the disconnect wording for an axios network error",
+ error: new AxiosError("Network Error"),
+ expected: CORS_OR_NETWORK_ERROR_MESSAGE,
+ },
+ {
+ description: "a plain Error's message",
+ error: new Error("Something specific broke"),
+ expected: "Something specific broke",
+ },
+ {
+ description: "the disconnect wording for a fetch failure",
+ error: new Error("Request failed: Failed to fetch", {
+ cause: new TypeError("Failed to fetch"),
+ }),
+ expected: CORS_OR_NETWORK_ERROR_MESSAGE,
+ },
+ {
+ description: "the timeout wording for a timed-out request",
+ error: new Error("Request timeout after 60000ms"),
+ expected: BACKEND_REQUEST_TIMEOUT_MESSAGE,
+ },
+ {
+ description: "a string error as is",
+ error: "Plain string failure",
+ expected: "Plain string failure",
+ },
+ {
+ description: "an empty string when nothing is usable",
+ error: {},
+ expected: "",
+ },
+ ])("keeps returning $description", ({ error, expected }) => {
+ expect(retrieveAxiosErrorMessage(error)).toBe(expected);
+ });
+});
diff --git a/src/utils/api-error-message.ts b/src/utils/api-error-message.ts
index 615c866b35..53dca51e94 100644
--- a/src/utils/api-error-message.ts
+++ b/src/utils/api-error-message.ts
@@ -1,6 +1,6 @@
import axios from "axios";
-import { retrieveAxiosErrorMessage } from "#/utils/retrieve-axios-error-message";
import { isSdkHttpError } from "#/utils/sdk-http-error";
+import { getUserFacingConnectionErrorMessage } from "#/utils/user-facing-error";
/**
* Extract the parsed response body from a failed API call.
@@ -125,5 +125,5 @@ export function getApiOrConnectionErrorMessage(
if (hasApiErrorResponse(error)) {
return getApiErrorMessage(error, fallback);
}
- return retrieveAxiosErrorMessage(error) || fallback;
+ return getUserFacingConnectionErrorMessage(error) || fallback;
}
diff --git a/src/utils/retrieve-axios-error-message.ts b/src/utils/retrieve-axios-error-message.ts
index 34227ffe82..753f225d4a 100644
--- a/src/utils/retrieve-axios-error-message.ts
+++ b/src/utils/retrieve-axios-error-message.ts
@@ -4,6 +4,8 @@ import {
isAxiosErrorWithMessageField,
} from "./type-guards";
import { getUserFacingConnectionErrorMessage } from "./user-facing-error";
+import { getApiErrorMessage } from "./api-error-message";
+import { isSdkHttpError } from "./sdk-http-error";
function isAxiosError(error: unknown): error is AxiosError {
return (
@@ -15,10 +17,17 @@ function isAxiosError(error: unknown): error is AxiosError {
}
/**
- * Retrieve the error message from an Axios error
+ * Retrieve the error message from an Axios error, an SDK `HttpError`, a plain
+ * `Error` or a string
* @param error The error to render a toast for
*/
export const retrieveAxiosErrorMessage = (error: unknown): string => {
+ // The shared TypeScript client's HttpError means the server answered. Its
+ // `message` is the raw `HTTP request failed (status): {json}` text, so read
+ // the server's message from the parsed body instead; an unusable body gives
+ // "" and the caller shows its own fallback.
+ if (isSdkHttpError(error)) return getApiErrorMessage(error, "");
+
let errorMessage: string | null = null;
let shouldPreferExtractedMessage = false;