Skip to content

Commit ff26b5a

Browse files
committed
refactor(server): separate launcher rejection errors
1 parent d393dab commit ff26b5a

3 files changed

Lines changed: 60 additions & 38 deletions

File tree

‎apps/server/src/cloud/selfUpdate.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
174174
.pipe(
175175
Effect.mapError((error) =>
176176
failWith(
177-
error.operation === "rejected"
177+
error._tag === "ServiceLauncherRejectedError"
178178
? error.reason
179179
: "Could not ask the service launcher to activate the prepared update.",
180180
error,

‎apps/server/src/cloud/serviceLauncherClient.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ it.effect("returns the launcher-generated ID only after update acceptance", () =
9595
}),
9696
);
9797

98+
it.effect("preserves a launcher rejection as a distinct error", () =>
99+
Effect.gen(function* () {
100+
const host = new FakeLauncherProcess({ protocol: 1, childVersion: "1.0.0" });
101+
const client = yield* makeClient(host, "1.0.0");
102+
const requested = yield* Effect.forkChild(client.requestUpdate({ targetVersion: "1.1.0" }), {
103+
startImmediately: true,
104+
});
105+
yield* Effect.yieldNow;
106+
host.emit({ type: "update-rejected", reason: "requires local update" });
107+
expect(yield* Fiber.join(requested).pipe(Effect.flip)).toMatchObject({
108+
_tag: "ServiceLauncherRejectedError",
109+
targetVersion: "1.1.0",
110+
reason: "requires local update",
111+
});
112+
}),
113+
);
114+
98115
it.effect("rejects contradictory trial context instead of leaving activation closed", () =>
99116
Effect.gen(function* () {
100117
const host = new FakeLauncherProcess({

‎apps/server/src/cloud/serviceLauncherClient.ts‎

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,36 @@ export class ServiceLauncherClientError extends Schema.TaggedErrorClass<ServiceL
2525
"send",
2626
"disconnect",
2727
"timeout",
28-
"rejected",
2928
]),
30-
reason: Schema.String,
3129
cause: Schema.optional(Schema.Defect()),
3230
},
31+
) {
32+
override get message(): string {
33+
switch (this.operation) {
34+
case "decode-context":
35+
return "The service launcher supplied invalid startup context.";
36+
case "version-mismatch":
37+
return "The service launcher started a different t3 version.";
38+
case "ipc-unavailable":
39+
return "The service launcher IPC channel is unavailable.";
40+
case "unmanaged":
41+
return "This server is not managed by the launcher.";
42+
case "send":
43+
return "Could not send a request to the service launcher.";
44+
case "disconnect":
45+
return "The service launcher disconnected before acknowledging the request.";
46+
case "timeout":
47+
return "The service launcher did not respond within 30 seconds.";
48+
}
49+
}
50+
}
51+
52+
export class ServiceLauncherRejectedError extends Schema.TaggedErrorClass<ServiceLauncherRejectedError>()(
53+
"ServiceLauncherRejectedError",
54+
{
55+
targetVersion: Schema.String,
56+
reason: Schema.String,
57+
},
3358
) {
3459
override get message(): string {
3560
return this.reason;
@@ -78,23 +103,14 @@ export class ServiceLauncherClient extends Context.Service<
78103
readonly trial: boolean;
79104
readonly requestUpdate: (input: {
80105
readonly targetVersion: string;
81-
}) => Effect.Effect<string, ServiceLauncherClientError>;
106+
}) => Effect.Effect<string, ServiceLauncherClientError | ServiceLauncherRejectedError>;
82107
readonly prepareTrial: Effect.Effect<
83108
ServerSelfUpdateOutcome | undefined,
84109
ServiceLauncherClientError
85110
>;
86111
}
87112
>()("t3/cloud/serviceLauncherClient") {}
88113

89-
const fail = (
90-
operation: ServiceLauncherClientError["operation"],
91-
reason: string,
92-
cause?: unknown,
93-
) =>
94-
cause === undefined
95-
? new ServiceLauncherClientError({ operation, reason })
96-
: new ServiceLauncherClientError({ operation, reason, cause });
97-
98114
const resolveStartup = Effect.fn("cloud.service_launcher_client.resolve_startup")(
99115
function* (options?: { readonly currentVersion?: string }) {
100116
const host = yield* ServiceLauncherHostProcess;
@@ -104,21 +120,15 @@ const resolveStartup = Effect.fn("cloud.service_launcher_client.resolve_startup"
104120
const context = rawContext === undefined ? undefined : decodeServiceLauncherContext(rawContext);
105121

106122
if (rawContext !== undefined && context === undefined) {
107-
return yield* fail(
108-
"decode-context",
109-
"The service launcher supplied invalid startup context.",
110-
);
123+
return yield* new ServiceLauncherClientError({ operation: "decode-context" });
111124
}
112125
if (context !== undefined && context.childVersion !== currentVersion) {
113-
return yield* fail(
114-
"version-mismatch",
115-
`The service launcher started t3@${context.childVersion}, but the child reports t3@${currentVersion}.`,
116-
);
126+
return yield* new ServiceLauncherClientError({ operation: "version-mismatch" });
117127
}
118128

119129
const managed = context !== undefined && host.connected;
120130
if (context !== undefined && !managed) {
121-
return yield* fail("ipc-unavailable", "The service launcher IPC channel is unavailable.");
131+
return yield* new ServiceLauncherClientError({ operation: "ipc-unavailable" });
122132
}
123133

124134
return { host, context, managed };
@@ -143,7 +153,7 @@ export const make = Effect.fn("cloud.service_launcher_client.make")(function* (o
143153
) =>
144154
Effect.callback<ServiceLauncherParentMessage, ServiceLauncherClientError>((resume) => {
145155
if (!managed) {
146-
resume(Effect.fail(fail("unmanaged", "This server is not managed by the launcher.")));
156+
resume(Effect.fail(new ServiceLauncherClientError({ operation: "unmanaged" })));
147157
return;
148158
}
149159

@@ -165,37 +175,27 @@ export const make = Effect.fn("cloud.service_launcher_client.make")(function* (o
165175
if (reply !== undefined && accept(reply)) settle(Effect.succeed(reply));
166176
};
167177
const onDisconnect = () =>
168-
settle(
169-
Effect.fail(
170-
fail(
171-
"disconnect",
172-
"The service launcher disconnected before acknowledging the request.",
173-
),
174-
),
175-
);
178+
settle(Effect.fail(new ServiceLauncherClientError({ operation: "disconnect" })));
176179

177180
host.on("message", onMessage);
178181
host.on("disconnect", onDisconnect);
179182
try {
180183
host.send(message, (error) => {
181184
if (error !== null) {
182185
settle(
183-
Effect.fail(fail("send", "Could not send a request to the service launcher.", error)),
186+
Effect.fail(new ServiceLauncherClientError({ operation: "send", cause: error })),
184187
);
185188
}
186189
});
187190
} catch (cause) {
188-
settle(
189-
Effect.fail(fail("send", "Could not send a request to the service launcher.", cause)),
190-
);
191+
settle(Effect.fail(new ServiceLauncherClientError({ operation: "send", cause })));
191192
}
192193

193194
return Effect.sync(cleanup);
194195
}).pipe(
195196
Effect.timeoutOrElse({
196197
duration: "30 seconds",
197-
orElse: () =>
198-
Effect.fail(fail("timeout", "The service launcher did not respond within 30 seconds.")),
198+
orElse: () => Effect.fail(new ServiceLauncherClientError({ operation: "timeout" })),
199199
}),
200200
);
201201

@@ -208,7 +208,12 @@ export const make = Effect.fn("cloud.service_launcher_client.make")(function* (o
208208
reply.type === "update-accepted"
209209
? Effect.succeed(reply.updateId)
210210
: reply.type === "update-rejected"
211-
? Effect.fail(fail("rejected", reply.reason))
211+
? Effect.fail(
212+
new ServiceLauncherRejectedError({
213+
targetVersion: input.targetVersion,
214+
reason: reply.reason,
215+
}),
216+
)
212217
: Effect.die("service launcher returned an impossible update response"),
213218
),
214219
);

0 commit comments

Comments
 (0)