Skip to content

Commit f785979

Browse files
[codex] Structure environment cloud HTTP errors (#3392)
Co-authored-by: codex <[email protected]>
1 parent 16dda36 commit f785979

5 files changed

Lines changed: 367 additions & 168 deletions

File tree

‎apps/mobile/src/features/cloud/linkEnvironment.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ describe("mobile cloud link environment client", () => {
607607
Response.json(
608608
{
609609
_tag: "EnvironmentHttpUnauthorizedError",
610-
message: "Invalid environment bearer session.",
610+
reason: "cloud_cli_authorization_required",
611+
message: "Run `t3 connect link` to authorize this environment.",
611612
},
612613
{ status: 401 },
613614
),
@@ -623,7 +624,7 @@ describe("mobile cloud link environment client", () => {
623624
).pipe(Effect.flip);
624625
expect(error._tag).toBe("CloudEnvironmentLinkError");
625626
expect(error.message).toBe(
626-
"Could not obtain environment link proof: Invalid environment bearer session.",
627+
"Could not obtain environment link proof: Run `t3 connect link` to authorize this environment.",
627628
);
628629
expect(fetchMock).toHaveBeenCalledTimes(2);
629630
}),

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ import {
2828
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
2929
import { traceAuthenticatedRelayRequest, traceRelayRequest } from "./traceRelayRequest.ts";
3030

31+
const encodeEnvironmentHttpInternalServerError = Schema.encodeUnknownSync(
32+
EnvironmentHttpInternalServerError,
33+
);
34+
const decodeEnvironmentHttpInternalServerError = Schema.decodeUnknownSync(
35+
EnvironmentHttpInternalServerError,
36+
);
37+
3138
const storeFailure = (tag: "AlreadyExists" | "PermissionDenied") =>
3239
new ServerSecretStore.SecretStorePersistError({
3340
operation: "create",
@@ -210,21 +217,31 @@ describe("CloudRelayRequestError", () => {
210217
});
211218
});
212219

213-
it("preserves internal causes without encoding them into HTTP error bodies", () => {
220+
it("keeps internal causes out of encoded HTTP error bodies", () => {
214221
const cause = new Error("private upstream detail");
215222
const error = new EnvironmentHttpInternalServerError({
216-
message: "Stable public failure.",
223+
operation: "generate_link_proof",
217224
cause,
218225
});
219-
const encodeError = Schema.encodeUnknownSync(EnvironmentHttpInternalServerError);
220226

221227
expect(error.cause).toBe(cause);
222-
expect(encodeError(error)).toEqual({
228+
expect(encodeEnvironmentHttpInternalServerError(error)).toEqual({
223229
_tag: "EnvironmentHttpInternalServerError",
224-
message: "Stable public failure.",
230+
operation: "generate_link_proof",
231+
message: "Could not generate environment link proof.",
225232
});
226233
});
227234

235+
it("decodes legacy message-only HTTP errors during rolling deployments", () => {
236+
const error = decodeEnvironmentHttpInternalServerError({
237+
_tag: "EnvironmentHttpInternalServerError",
238+
message: "Legacy environment server failure.",
239+
});
240+
241+
expect(error.operation).toBeUndefined();
242+
expect(error.message).toBe("Legacy environment server failure.");
243+
});
244+
228245
describe("relay request tracing", () => {
229246
it.effect("does not accept an unauthenticated request trace parent", () =>
230247
Effect.gen(function* () {
@@ -298,6 +315,7 @@ describe("reconcileDesiredCloudLink", () => {
298315

299316
expect(error).toMatchObject({
300317
_tag: "EnvironmentHttpUnauthorizedError",
318+
reason: "cloud_cli_authorization_required",
301319
message: "Run `t3 connect link` to authorize this environment.",
302320
});
303321
}),
@@ -334,6 +352,9 @@ describe("reconcileDesiredCloudLink", () => {
334352

335353
expect(error).toMatchObject({
336354
_tag: "EnvironmentHttpInternalServerError",
355+
operation: "relay_request",
356+
relayOperation: "create-link-challenge",
357+
relayPhase: "send-request",
337358
message: "T3 Connect relay create-link-challenge failed during send-request.",
338359
cause: {
339360
_tag: "CloudRelayRequestError",
@@ -346,7 +367,12 @@ describe("reconcileDesiredCloudLink", () => {
346367
const logFields = capturedLogs[0]?.find(
347368
(value): value is Record<string, unknown> => typeof value === "object" && value !== null,
348369
);
349-
expect(logFields).toMatchObject({ causeTag: "CloudRelayRequestError" });
370+
expect(logFields).toMatchObject({
371+
operation: "relay_request",
372+
relayOperation: "create-link-challenge",
373+
relayPhase: "send-request",
374+
causeTag: "CloudRelayRequestError",
375+
});
350376
expect(logFields).not.toHaveProperty("cause");
351377
expect(capturedLogs[0]?.filter((value) => typeof value === "string").join(" ")).not.toContain(
352378
transportCause.message,

0 commit comments

Comments
 (0)