Preserve SSH and network error messages

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-06-20 20:17:07 -07:00
1 parent 7e85f7b6ec
commit 57b8f5654e
8 files changed
+84 -47

No files matched your search

@@ -2,7 +2,6 @@ import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as NetService from "@t3tools/shared/Net";
import { SshPasswordPromptRequestError } from "@t3tools/ssh/errors";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
@@ -39,12 +38,10 @@ describe("sshEnvironment", () => {
requestId: "prompt-1",
destination: "devbox",
});
const error = new SshPasswordPromptRequestError({
destination: "devbox",
cause,
});
assert.strictEqual(error.cause, cause);
const error = DesktopSshEnvironment.toSshPasswordPromptError(cause);
assert(DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation(error));
assert.strictEqual(error.cause, cause);
assert.equal(error.message, "SSH authentication timed out for devbox.");
assert.equal(error.cause.message, "SSH authentication timed out for devbox.");
});
+5
View File
@@ -45,6 +45,11 @@ const openServer = (host?: string): Effect.Effect<NodeNet.Server, NetService.Net
it.layer(NetService.layer)("NetService", (it) => {
describe("Net helpers", () => {
it("preserves the loopback reservation error message", () => {
const error = new NetService.NetError({ host: "127.0.0.1" });
assert.equal(error.message, "Failed to reserve loopback port");
});
it.effect("reserveLoopbackPort returns a positive loopback port", () =>
Effect.gen(function* () {
const net = yield* NetService.NetService;
+1 -1
View File
@@ -11,7 +11,7 @@ export class NetError extends Schema.TaggedErrorClass<NetError>()("NetError", {
cause: Schema.optional(Schema.Defect()),
}) {
override get message(): string {
return `Failed to reserve loopback port on ${this.host}.`;
return "Failed to reserve loopback port";
}
}
+3 -24
View File
@@ -17,7 +17,7 @@ import {
resolveRemoteT3CliPackageSpec,
runSshCommand,
} from "./command.ts";
import { SshCommandExitError, SshCommandSpawnError, SshTunnelExitError } from "./errors.ts";
import { SshCommandExitError, SshCommandSpawnError } from "./errors.ts";
const encoder = new TextEncoder();
@@ -173,27 +173,7 @@ describe("ssh command", () => {
assert.equal(result.failure.stderr, "");
}
assert.equal(
new SshCommandExitError({
command: ["ssh"],
exitCode: 1,
stderr: "",
stdout: "",
target: "devbox",
}).message,
"SSH command failed for devbox (exit 1).",
);
assert.equal(
new SshTunnelExitError({
command: ["ssh"],
exitCode: 1,
stderr: "",
target: "devbox",
}).message,
"SSH tunnel exited unexpectedly for devbox (exit 1).",
);
const spawnCause = new Error("ssh executable path included sensitive diagnostics");
const spawnCause = new Error("ssh executable was not found");
const spawnError = new SshCommandSpawnError({
command: ["ssh"],
exitCode: null,
@@ -202,8 +182,7 @@ describe("ssh command", () => {
cause: spawnCause,
});
assert.strictEqual(spawnError.cause, spawnCause);
assert.equal(spawnError.message, "Failed to spawn SSH command for devbox.");
assert.notInclude(spawnError.message, spawnCause.message);
assert.equal(spawnError.message, "ssh executable was not found");
}).pipe(Effect.provide(processLayer));
});
+1 -1
View File
@@ -24,7 +24,7 @@ describe("ssh config", () => {
const error = new SshHostDiscoveryError({ homeDir: "/home/test", cause });
assert.strictEqual(error.cause, cause);
assert.equal(error.message, "SSH host discovery failed for /home/test.");
assert.equal(error.message, "Failed to discover SSH hosts.");
assert.notInclude(error.message, cause.message);
});
+25 -11
View File
@@ -8,7 +8,7 @@ export class SshHostDiscoveryError extends Schema.TaggedErrorClass<SshHostDiscov
},
) {
override get message(): string {
return `SSH host discovery failed${this.homeDir === null ? "" : ` for ${this.homeDir}`}.`;
return "Failed to discover SSH hosts.";
}
}
@@ -62,7 +62,7 @@ export class SshAuthenticationHelperError extends Schema.TaggedErrorClass<SshAut
},
) {
override get message(): string {
return `Failed to prepare SSH authentication helpers${targetSuffix(this.target)}.`;
return "Failed to prepare SSH authentication helpers.";
}
}
@@ -73,7 +73,7 @@ export class SshCommandSpawnError extends Schema.TaggedErrorClass<SshCommandSpaw
},
) {
override get message(): string {
return `Failed to spawn SSH command${targetSuffix(this.target)}.`;
return causeMessage(this.cause, `Failed to spawn SSH command${targetSuffix(this.target)}.`);
}
}
@@ -84,7 +84,7 @@ export class SshCommandExecutionError extends Schema.TaggedErrorClass<SshCommand
},
) {
override get message(): string {
return `Failed to run SSH command${targetSuffix(this.target)}.`;
return causeMessage(this.cause, `Failed to run SSH command${targetSuffix(this.target)}.`);
}
}
@@ -132,7 +132,7 @@ export class SshTunnelSpawnError extends Schema.TaggedErrorClass<SshTunnelSpawnE
},
) {
override get message(): string {
return `Failed to spawn SSH tunnel${targetSuffix(this.target)}.`;
return causeMessage(this.cause, `Failed to spawn SSH tunnel${targetSuffix(this.target)}.`);
}
}
@@ -143,7 +143,7 @@ export class SshTunnelMonitorError extends Schema.TaggedErrorClass<SshTunnelMoni
},
) {
override get message(): string {
return `Failed to monitor SSH tunnel${targetSuffix(this.target)}.`;
return causeMessage(this.cause, `Failed to monitor SSH tunnel${targetSuffix(this.target)}.`);
}
}
@@ -258,15 +258,23 @@ export const SshPairingError = Schema.Union([
]);
export type SshPairingError = typeof SshPairingError.Type;
export class SshHttpBridgeMissingUrlError extends Schema.TaggedErrorClass<SshHttpBridgeMissingUrlError>()(
"SshHttpBridgeMissingUrlError",
{},
) {
override get message(): string {
return "Invalid SSH forwarded http base URL.";
}
}
export class SshHttpBridgeInvalidUrlError extends Schema.TaggedErrorClass<SshHttpBridgeInvalidUrlError>()(
"SshHttpBridgeInvalidUrlError",
{
reason: Schema.Literals(["missing_url", "invalid_url"]),
cause: Schema.optional(Schema.Defect()),
cause: Schema.Defect(),
},
) {
override get message(): string {
return "Invalid SSH forwarded HTTP base URL.";
return causeMessage(this.cause, "Invalid SSH forwarded http base URL.");
}
}
@@ -277,11 +285,12 @@ export class SshHttpBridgeNonLoopbackUrlError extends Schema.TaggedErrorClass<Ss
},
) {
override get message(): string {
return `SSH desktop bridge only supports loopback forwarded URLs${targetSuffix(this.hostname)}.`;
return "SSH desktop bridge only supports loopback forwarded URLs.";
}
}
export const SshHttpBridgeError = Schema.Union([
SshHttpBridgeMissingUrlError,
SshHttpBridgeInvalidUrlError,
SshHttpBridgeNonLoopbackUrlError,
]);
@@ -315,6 +324,7 @@ export class SshReadinessProbeTimeoutError extends Schema.TaggedErrorClass<SshRe
export class SshReadinessTimeoutError extends Schema.TaggedErrorClass<SshReadinessTimeoutError>()(
"SshReadinessTimeoutError",
{
baseUrl: Schema.String,
requestUrl: Schema.String,
timeoutMs: Schema.Number,
attempts: Schema.Number,
@@ -322,7 +332,7 @@ export class SshReadinessTimeoutError extends Schema.TaggedErrorClass<SshReadine
},
) {
override get message(): string {
return `Timed out waiting ${this.timeoutMs}ms for backend readiness at ${this.requestUrl}.`;
return `Timed out waiting ${this.timeoutMs}ms for backend readiness at ${this.baseUrl}.`;
}
}
@@ -444,6 +454,10 @@ function targetSuffix(target: string | undefined): string {
return target ? ` for ${target}` : "";
}
function causeMessage(cause: unknown, fallback: string): string {
return cause instanceof Error ? cause.message : fallback;
}
function exitCodeSuffix(exitCode: number | null): string {
return exitCode === null ? "" : ` (exit ${exitCode})`;
}
+42 -2
View File
@@ -15,7 +15,13 @@ import * as ChildProcess from "effect/unstable/process/ChildProcess";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
import * as SshAuth from "./auth.ts";
import { SshReadinessProbeTimeoutError, SshReadinessTimeoutError } from "./errors.ts";
import {
SshHttpBridgeInvalidUrlError,
SshHttpBridgeMissingUrlError,
SshHttpBridgeNonLoopbackUrlError,
SshReadinessProbeTimeoutError,
SshReadinessTimeoutError,
} from "./errors.ts";
import * as SshTunnel from "./tunnel.ts";
const TEST_NODE_ENGINE_RANGE = "^22.16 || ^23.11 || >=24.10";
@@ -269,8 +275,12 @@ describe("ssh tunnel scripts", () => {
if (Result.isFailure(result)) {
assert.instanceOf(result.failure, SshReadinessTimeoutError);
const timeoutError = result.failure as SshReadinessTimeoutError;
assert.equal(timeoutError.baseUrl, "http://127.0.0.1:41773/");
assert.equal(timeoutError.requestUrl, "http://127.0.0.1:41773/ready");
assert.include(timeoutError.message, "Timed out waiting 1000ms");
assert.equal(
timeoutError.message,
"Timed out waiting 1000ms for backend readiness at http://127.0.0.1:41773/.",
);
assert.isAbove(timeoutError.attempts, 0);
assert.instanceOf(timeoutError.cause, SshReadinessProbeTimeoutError);
const probeTimeout = timeoutError.cause as SshReadinessProbeTimeoutError;
@@ -284,6 +294,36 @@ describe("ssh tunnel scripts", () => {
),
);
it.effect("preserves forwarded HTTP URL error messages", () =>
Effect.gen(function* () {
const missing = yield* Effect.result(SshTunnel.resolveLoopbackSshHttpBaseUrl(""));
assert.isTrue(Result.isFailure(missing));
if (Result.isFailure(missing)) {
assert.instanceOf(missing.failure, SshHttpBridgeMissingUrlError);
assert.equal(missing.failure.message, "Invalid SSH forwarded http base URL.");
}
const invalid = yield* Effect.result(SshTunnel.resolveLoopbackSshHttpBaseUrl("not-a-url"));
assert.isTrue(Result.isFailure(invalid));
if (Result.isFailure(invalid)) {
assert.instanceOf(invalid.failure, SshHttpBridgeInvalidUrlError);
assert.equal(invalid.failure.message, "Invalid URL");
}
const nonLoopback = yield* Effect.result(
SshTunnel.resolveLoopbackSshHttpBaseUrl("https://example.com"),
);
assert.isTrue(Result.isFailure(nonLoopback));
if (Result.isFailure(nonLoopback)) {
assert.instanceOf(nonLoopback.failure, SshHttpBridgeNonLoopbackUrlError);
assert.equal(
nonLoopback.failure.message,
"SSH desktop bridge only supports loopback forwarded URLs.",
);
}
}),
);
it("preserves primitive readiness reason values in diagnostic output", () => {
assert.deepEqual(
SshTunnel.describeReadinessCause({
+4 -2
View File
@@ -41,6 +41,7 @@ import {
SshCommandCancelledError,
SshCommandError,
SshHttpBridgeInvalidUrlError,
SshHttpBridgeMissingUrlError,
SshHttpBridgeNonLoopbackUrlError,
SshHttpBridgeError,
SshInvalidTargetError,
@@ -979,6 +980,7 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
lastFailure: describeReadinessCause(lastFailure),
});
return yield* new SshReadinessTimeoutError({
baseUrl: input.baseUrl,
requestUrl,
timeoutMs,
attempts: attempt,
@@ -999,11 +1001,11 @@ function isLoopbackHostname(hostname: string): boolean {
export const resolveLoopbackSshHttpBaseUrl = Effect.fn("ssh/tunnel.resolveLoopbackSshHttpBaseUrl")(
function* (rawHttpBaseUrl: unknown): Effect.fn.Return<string, SshHttpBridgeError> {
if (typeof rawHttpBaseUrl !== "string" || rawHttpBaseUrl.trim().length === 0) {
return yield* new SshHttpBridgeInvalidUrlError({ reason: "missing_url" });
return yield* new SshHttpBridgeMissingUrlError();
}
const baseUrl = yield* Effect.try({
try: () => new URL(rawHttpBaseUrl),
catch: (cause) => new SshHttpBridgeInvalidUrlError({ reason: "invalid_url", cause }),
catch: (cause) => new SshHttpBridgeInvalidUrlError({ cause }),
});
if (!isLoopbackHostname(baseUrl.hostname)) {
return yield* new SshHttpBridgeNonLoopbackUrlError({