Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

relayResponse.ts

82 lines3.6 KB

1import {
2 EnvironmentHttpBadRequestError,
3 EnvironmentHttpConflictError,
4 EnvironmentHttpForbiddenError,
5 EnvironmentHttpInternalServerError,
6 EnvironmentHttpUnauthorizedError,
7} from "@t3tools/contracts";
8import { RelayProtectedError } from "@t3tools/contracts/relay";
9import * as Effect from "effect/Effect";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
13import { isHttpClientError } from "effect/unstable/http/HttpClientError";
14​
15const isRelayResponseError = Schema.is(
16 Schema.Union([
17 EnvironmentHttpBadRequestError,
18 EnvironmentHttpForbiddenError,
19 EnvironmentHttpInternalServerError,
20 EnvironmentHttpUnauthorizedError,
21 ]),
22);
23​
24export function relayRequestError(cause: unknown) {
25 return isRelayResponseError(cause)
26 ? cause
27 : new EnvironmentHttpInternalServerError({
28 message: `Could not complete the T3 Connect relay request. ${isHttpClientError(cause) ? `The relay request failed (${cause.reason._tag}).` : "The relay returned an unexpected response."} Check this machine's network connection and relay availability, then retry.`,
29 });
31​
32const isPermanentCloudLinkError = Schema.is(
33 Schema.Union([
34 EnvironmentHttpBadRequestError,
35 EnvironmentHttpForbiddenError,
36 EnvironmentHttpUnauthorizedError,
37 EnvironmentHttpConflictError,
38 ]),
39);
40​
41export const shouldRetryCloudLink = (error: unknown): boolean => !isPermanentCloudLinkError(error);
42​
43function recoveryHint(error: RelayProtectedError): string {
44 switch (error._tag) {
45 case "RelayEnvironmentLinkLimitExceededError":
46 return "Unlink an unused environment in T3 Connect, then restart T3 Code on this machine.";
47 case "RelayAuthInvalidError":
48 return "Run `t3 connect login` to check this machine's authorization. If the stored credential was revoked, sign out with `t3 connect logout`, then run `t3 connect` again. Restart T3 Code after signing in.";
49 case "RelayEnvironmentLinkProofExpiredError":
50 case "RelayEnvironmentLinkProofInvalidError":
51 return "Check this machine's date and time, update T3 Code, then restart it.";
52 default:
53 return "Retry when the relay is available. If this continues, include the trace ID when reporting it.";
54 }
56​
57/** Preserve relay diagnostics before converting permanent rejections into non-retryable errors. */
58export const filterRelayResponse = Effect.fn("cloud.filter_relay_response")(function* (
59 response: HttpClientResponse.HttpClientResponse,
60) {
61 if (response.status >= 200 && response.status < 300) return response;
62 const decoded = yield* HttpClientResponse.schemaBodyJson(RelayProtectedError)(response).pipe(
63 Effect.option,
64 );
65 const ray = response.headers["cf-ray"];
66 const requestId = ray && /^[a-zA-Z0-9-]{1,128}$/.test(ray) ? ` Cloudflare Ray ID: ${ray}.` : "";
67 const message = Option.isSome(decoded)
68 ? `T3 Connect: ${decoded.value.message}. ${recoveryHint(decoded.value)} Trace ID: ${decoded.value.traceId}.`
69 : `T3 Connect relay returned HTTP ${response.status} without a recognized error response. Check relay access and any proxy or firewall restrictions, then restart T3 Code.${requestId}`;
70​
71 if (response.status === 401) return yield* new EnvironmentHttpUnauthorizedError({ message });
72 if (response.status === 403) return yield* new EnvironmentHttpForbiddenError({ message });
73 if (
74 response.status >= 400 &&
75 response.status < 500 &&
76 response.status !== 408 &&
77 response.status !== 429
78 ) {
79 return yield* new EnvironmentHttpBadRequestError({ message });
80 }
81 return yield* new EnvironmentHttpInternalServerError({ message });
82});