Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(canvas-extensions): show the install failure reason (#17120)

Vasco Schiavo6f240ccbdced
diff --git a/__tests__/hooks/mutation/use-install-canvas-extension.test.ts b/__tests__/hooks/mutation/use-install-canvas-extension.test.ts
new file mode 100644
index 0000000000..d7085908ef
--- /dev/null
+++ b/__tests__/hooks/mutation/use-install-canvas-extension.test.ts
@@ -0,0 +1,79 @@
+import React from "react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { renderHook, waitFor } from "@testing-library/react";
+import { describe, expect, it, vi, beforeEach } from "vitest";
+import CanvasExtensionsService from "#/api/canvas-extensions-service";
+import { useInstallCanvasExtension } from "#/hooks/mutation/use-manage-canvas-extensions";
+import { CORS_OR_NETWORK_ERROR_MESSAGE } from "#/utils/user-facing-error";
+
+const displayErrorToast = vi.fn();
+vi.mock("#/utils/custom-toast-handlers", () => ({
+ displayErrorToast: (message: string) => displayErrorToast(message),
+ displaySuccessToast: vi.fn(),
+}));
+
+const createWrapper = () => {
+ const client = new QueryClient({
+ defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
+ });
+ return function Wrapper({ children }: { children: React.ReactNode }) {
+ return React.createElement(QueryClientProvider, { client }, children);
+ };
+};
+
+/** The shape the shared TypeScript client throws for a failed request. */
+class HttpError extends Error {
+ status: number;
+
+ response: unknown;
+
+ constructor(status: number, detail: string) {
+ super(
+ `HTTP request failed (${status} Bad Request): ${JSON.stringify({ detail })}`,
+ );
+ this.name = "HttpError";
+ this.status = status;
+ this.response = { detail };
+ }
+}
+
+describe("useInstallCanvasExtension", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("surfaces the server's reason instead of the raw HTTP message", async () => {
+ const detail =
+ "Failed to fetch canvas extension source: Subdirectory 'canvas-puls' not found in local source '/repo'";
+ vi.spyOn(CanvasExtensionsService, "install").mockRejectedValue(
+ new HttpError(400, detail),
+ );
+
+ const { result } = renderHook(() => useInstallCanvasExtension(), {
+ wrapper: createWrapper(),
+ });
+ result.current.mutate({ source: "/repo", repo_path: "canvas-puls" });
+
+ await waitFor(() => expect(displayErrorToast).toHaveBeenCalled());
+ expect(displayErrorToast).toHaveBeenCalledWith(detail);
+ expect(displayErrorToast).not.toHaveBeenCalledWith(
+ expect.stringContaining("HTTP request failed"),
+ );
+ });
+
+ it("keeps the shared disconnect wording when the request never lands", async () => {
+ vi.spyOn(CanvasExtensionsService, "install").mockRejectedValue(
+ new TypeError("Failed to fetch"),
+ );
+
+ const { result } = renderHook(() => useInstallCanvasExtension(), {
+ wrapper: createWrapper(),
+ });
+ result.current.mutate({ source: "/repo" });
+
+ await waitFor(() => expect(displayErrorToast).toHaveBeenCalled());
+ expect(displayErrorToast).toHaveBeenCalledWith(
+ CORS_OR_NETWORK_ERROR_MESSAGE,
+ );
+ });
+});
diff --git a/src/hooks/mutation/use-manage-canvas-extensions.ts b/src/hooks/mutation/use-manage-canvas-extensions.ts
index 6f81fb0903..726f550f46 100644
--- a/src/hooks/mutation/use-manage-canvas-extensions.ts
+++ b/src/hooks/mutation/use-manage-canvas-extensions.ts
@@ -4,7 +4,12 @@ import CanvasExtensionsService from "#/api/canvas-extensions-service";
import type { InstallCanvasExtensionRequest } from "#/types/canvas-extension";
import { CANVAS_EXTENSIONS_QUERY_KEYS } from "#/hooks/query/query-keys";
import { I18nKey } from "#/i18n/declaration";
-import { displaySuccessToast } from "#/utils/custom-toast-handlers";
+import {
+ displayErrorToast,
+ displaySuccessToast,
+} from "#/utils/custom-toast-handlers";
+import { getApiErrorBody, getApiErrorMessage } from "#/utils/api-error-message";
+import { retrieveAxiosErrorMessage } from "#/utils/retrieve-axios-error-message";
function useInvalidateCanvasExtensions() {
const queryClient = useQueryClient();
@@ -18,12 +23,23 @@ export function useInstallCanvasExtension() {
const invalidate = useInvalidateCanvasExtensions();
const { t } = useTranslation("openhands");
return useMutation({
+ // The default toast prints the client's raw `HTTP 400: {...}` message;
+ // the server's `detail` says which of Source/Ref/Path is actually wrong.
+ meta: { disableToast: true },
mutationFn: (request: InstallCanvasExtensionRequest) =>
CanvasExtensionsService.install(request),
onSuccess: () => {
void invalidate();
displaySuccessToast(t(I18nKey.SETTINGS$APPS_INSTALL_SUCCESS));
},
+ onError: (error) => {
+ // A transport failure carries no response body, so keep the shared
+ // "Disconnected" wording for it and use the server's detail otherwise.
+ const message = getApiErrorBody(error)
+ ? getApiErrorMessage(error, t(I18nKey.ERROR$GENERIC))
+ : retrieveAxiosErrorMessage(error) || t(I18nKey.ERROR$GENERIC);
+ displayErrorToast(message);
+ },
});
}