TypeScript
fix(web): device host checks show the error without a stack trace (#29912)
Alex Southwell3ee55151e075
diff --git a/apps/web/src/components/settings/useHostConnectionChecks.browser.test.tsx b/apps/web/src/components/settings/useHostConnectionChecks.browser.test.tsxnew file mode 100644index 00000000000..93c2776034a--- /dev/null+++ b/apps/web/src/components/settings/useHostConnectionChecks.browser.test.tsx@@ -0,0 +1,64 @@+import { DeviceHostUnavailableError, EnvironmentId } from "@t3tools/contracts";+import * as Cause from "effect/Cause";+import { AsyncResult } from "effect/reactivity";+import { describe, expect, it, vi } from "vite-plus/test";++import { act, render } from "../../../test/render";++const state = vi.hoisted(() => ({ test: vi.fn() }));+function setup() {+ state.test.mockReset();+}++vi.mock(import("../../state/use-atom-command"), async (original) => ({+ ...(await original()),+ useAtomCommand: (() => state.test) as never,+}));+vi.mock(import("../../state/session"), async (original) => ({+ ...(await original()),+ readEnvironmentScope: (() => true) as never,+}));++import { useHostConnectionChecks } from "./useHostConnectionChecks";++const environmentId = EnvironmentId.make("environment-1");+const targets = [{ environmentId, label: "This Mac", connected: true }];++function Harness({+ onReady,+}: {+ onReady: (run: ReturnType<typeof useHostConnectionChecks>) => void;+}) {+ onReady(useHostConnectionChecks(targets));+ return null;+}++describe("useHostConnectionChecks", () => {+ it("reports a failed check by its message, without a stack trace", async () => {+ setup();+ state.test.mockResolvedValue(+ AsyncResult.failure(+ Cause.fail(+ new DeviceHostUnavailableError({+ hostId: "mac",+ hostLabel: "Mac mini",+ reason: "SSH connection refused.",+ }),+ ),+ ),+ );+ let checks!: ReturnType<typeof useHostConnectionChecks>;+ await using _screen = await render(<Harness onReady={(next) => (checks = next)} />, {+ router: false, // the hook reads no route state+ });++ const results = await act(() =>+ checks.testConnection({ kind: "ssh", id: "mac", label: "Mac mini", target: "user@mac" }),+ );++ expect(results?.[environmentId]).toEqual({+ status: "failed",+ error: "Mac mini is unavailable: SSH connection refused.",+ });+ });+});diff --git a/apps/web/src/components/settings/useHostConnectionChecks.ts b/apps/web/src/components/settings/useHostConnectionChecks.tsindex 4b2733cdfcf..e96b992a149 100644--- a/apps/web/src/components/settings/useHostConnectionChecks.ts+++ b/apps/web/src/components/settings/useHostConnectionChecks.ts@@ -1,7 +1,7 @@import { AuthSettingsWriteScope } from "@t3tools/contracts";import { readEnvironmentScope } from "../../state/session";import { useRef, useState } from "react";-import * as Cause from "effect/Cause";+import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";import type { SshDeviceHostConfig } from "@t3tools/contracts";import { deviceEnvironment } from "../../state/device";import { useAtomCommand } from "../../state/use-atom-command";@@ -30,7 +30,7 @@ export function useHostConnectionChecks(targets: ReadonlyArray<DeviceHostCheckTathrow new Error("This connection cannot test device hosts.");}const result = await test({ environmentId, input });- if (result._tag === "Failure") throw new Error(Cause.pretty(result.cause));+ if (result._tag === "Failure") throw squashAtomCommandFailure(result);return result.value;},(environmentId, result) => {