Skip to content

Commit 5c215dd

Browse files
committed
fix(desktop): clarify existing server connection errors
1 parent 07d6090 commit 5c215dd

2 files changed

Lines changed: 72 additions & 10 deletions

File tree

‎apps/desktop/src/backend/DesktopExistingServer.test.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Effect from "effect/Effect";
33
import * as FileSystem from "effect/FileSystem";
44
import * as Layer from "effect/Layer";
55
import * as Option from "effect/Option";
6+
import * as PlatformError from "effect/PlatformError";
67
import { HttpClient, HttpClientResponse } from "effect/http";
78
import { ChildProcessSpawner } from "effect/process";
89

@@ -35,9 +36,22 @@ const config: DesktopBackendStartConfig = {
3536
};
3637

3738
describe("connectExistingServer", () => {
38-
it.effect("refuses incompatible or mismatched servers before issuing a credential", () =>
39+
it.effect("reports recovery advice for the failed attachment step", () =>
3940
Effect.gen(function* () {
40-
for (const scenario of ["older", "legacy", "dev", "different-home", "unreachable"] as const) {
41+
const spawnError = PlatformError.systemError({
42+
_tag: "PermissionDenied",
43+
module: "ChildProcess",
44+
method: "spawn",
45+
cause: new Error("Permission denied"),
46+
});
47+
for (const scenario of [
48+
"older",
49+
"legacy",
50+
"dev",
51+
"different-home",
52+
"unreachable",
53+
"session",
54+
] as const) {
4155
const result = yield* connectExistingServer(config).pipe(
4256
Effect.provide(
4357
Layer.mergeAll(
@@ -84,14 +98,31 @@ describe("connectExistingServer", () => {
8498
Layer.succeed(
8599
ChildProcessSpawner.ChildProcessSpawner,
86100
ChildProcessSpawner.make(() =>
87-
Effect.die("must not issue a credential for an unverified server"),
101+
scenario === "session"
102+
? Effect.fail(spawnError)
103+
: Effect.die("must not issue a credential for an unverified server"),
88104
),
89105
),
90106
),
91107
),
92108
Effect.result,
93109
);
94110
assert.equal(result._tag, "Failure", scenario);
111+
if (result._tag === "Failure") {
112+
if (scenario === "unreachable" || scenario === "session") {
113+
assert.notInclude(result.failure.message, "Update or stop");
114+
}
115+
if (scenario === "session") {
116+
assert.include(result.failure.message, "session");
117+
assert.strictEqual(result.failure.cause, spawnError);
118+
}
119+
if (scenario === "older") {
120+
assert.include(result.failure.message, "Update or stop");
121+
}
122+
if (scenario === "different-home") {
123+
assert.include(result.failure.message, "different environment");
124+
}
125+
}
95126
}
96127
}),
97128
);

‎apps/desktop/src/backend/DesktopExistingServer.ts‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@ import type { DesktopBackendStartConfig } from "./DesktopBackendManager.ts";
2020

2121
export class DesktopExistingServerError extends Schema.TaggedError<DesktopExistingServerError>()(
2222
"DesktopExistingServerError",
23-
{ cause: Schema.Defect() },
23+
{
24+
reason: Schema.Literals([
25+
"incompatible-server",
26+
"different-environment",
27+
"session-issue-failed",
28+
"server-replaced",
29+
"connect-failed",
30+
]),
31+
cause: Schema.optionalKey(Schema.Defect()),
32+
},
2433
) {
2534
override get message() {
26-
return "T3 Code could not connect to the server already using this T3 home. Update or stop that server, then reopen the app.";
35+
switch (this.reason) {
36+
case "incompatible-server":
37+
return "T3 Code cannot connect to the server already using this T3 home because it is incompatible. Update or stop that server, then reopen the app.";
38+
case "different-environment":
39+
return "T3 Code found a different environment at the existing server address. Check the server address and T3 home, then reopen the app.";
40+
case "session-issue-failed":
41+
return "T3 Code could not create a desktop session for the existing server. Check the desktop logs for details, then reopen the app.";
42+
case "server-replaced":
43+
return "The existing T3 Code server changed while the app was connecting. Reopen the app to connect again.";
44+
case "connect-failed":
45+
return "T3 Code could not connect to the server already using this T3 home. Check that the server is running and inspect the desktop logs, then reopen the app.";
46+
}
2747
}
2848
}
2949

@@ -45,7 +65,7 @@ export const connectExistingServer = Effect.fn("desktop.existingServer.connect")
4565
const running = runtime.value;
4666
// The bundled CLI opens auth storage; an older server must never get newer migrations.
4767
if (!running.ownerId || running.devUrl || running.serverVersion !== serverPackageJson.version) {
48-
return yield* new DesktopExistingServerError({ cause: "incompatible-server" });
68+
return yield* new DesktopExistingServerError({ reason: "incompatible-server" });
4969
}
5070
const fs = yield* FileSystem.FileSystem;
5171
const environmentId = (yield* fs.readFileString(
@@ -62,7 +82,7 @@ export const connectExistingServer = Effect.fn("desktop.existingServer.connect")
6282
descriptor.environmentId !== environmentId ||
6383
descriptor.serverVersion !== running.serverVersion
6484
) {
65-
return yield* new DesktopExistingServerError({ cause: "different-environment" });
85+
return yield* new DesktopExistingServerError({ reason: "different-environment" });
6686
}
6787
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
6888
const session = yield* Effect.scoped(
@@ -95,13 +115,22 @@ export const connectExistingServer = Effect.fn("desktop.existingServer.connect")
95115
{ concurrency: "unbounded" },
96116
);
97117
if (code !== 0)
98-
return yield* new DesktopExistingServerError({ cause: "session-issue-failed" });
118+
return yield* new DesktopExistingServerError({
119+
reason: "session-issue-failed",
120+
cause: code,
121+
});
99122
return yield* decodeSession(stdout);
100123
}),
124+
).pipe(
125+
Effect.mapError((cause) =>
126+
isDesktopExistingServerError(cause)
127+
? cause
128+
: new DesktopExistingServerError({ reason: "session-issue-failed", cause }),
129+
),
101130
);
102131
const latest = yield* readPersistedServerRuntimeState(statePath);
103132
if (Option.isNone(latest) || latest.value.ownerId !== running.ownerId) {
104-
return yield* new DesktopExistingServerError({ cause: "server-replaced" });
133+
return yield* new DesktopExistingServerError({ reason: "server-replaced" });
105134
}
106135
return Option.some({
107136
...config,
@@ -111,6 +140,8 @@ export const connectExistingServer = Effect.fn("desktop.existingServer.connect")
111140
},
112141
Effect.timeout(Duration.minutes(1)),
113142
Effect.mapError((cause) =>
114-
isDesktopExistingServerError(cause) ? cause : new DesktopExistingServerError({ cause }),
143+
isDesktopExistingServerError(cause)
144+
? cause
145+
: new DesktopExistingServerError({ reason: "connect-failed", cause }),
115146
),
116147
);

0 commit comments

Comments
 (0)