Open History to see the last commit
1import { RelayProtectedError } from "@t3tools/contracts/relay";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5import * as HttpClientResponse from "effect/http/HttpClientResponse";
6import { isHttpClientError } from "effect/http/HttpClientError";
7
8/**
9 * `unavailable` means it may succeed on retry; the relay refused the other
10 * kinds. The description is safe to show: it carries the relay's own
11 * explanation and trace ID, never request details.
12 */
13export class RelayRequestError extends Schema.TaggedError<RelayRequestError>()(
14 "RelayRequestError",
15 {
16 rejection: Schema.Literals(["unauthorized", "forbidden", "rejected", "unavailable"]),
17 description: Schema.String,
18 },
19) {
20 override get message(): string {
21 return this.description;
22 }
23}
24
25const isRelayRequestError = Schema.is(RelayRequestError);
26
27export function relayRequestError(cause: unknown): RelayRequestError {
28 return isRelayRequestError(cause)
29 ? cause
30 : new RelayRequestError({
31 rejection: "unavailable",
32 description: `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.`,
33 });
34}
35
36export const shouldRetryRelayRequest = (error: unknown): boolean =>
37 !isRelayRequestError(error) || error.rejection === "unavailable";
38
39function recoveryHint(error: RelayProtectedError): string {
40 switch (error._tag) {
41 case "RelayEnvironmentLinkLimitExceededError":
42 return "Unlink an unused environment in T3 Connect, then restart T3 Code on this machine.";
43 case "RelayAuthInvalidError":
44 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.";
45 case "RelayEnvironmentLinkProofExpiredError":
46 case "RelayEnvironmentLinkProofInvalidError":
47 return "Check this machine's date and time, update T3 Code, then restart it.";
48 default:
49 return "Retry when the relay is available. If this continues, include the trace ID when reporting it.";
50 }
51}
52
53export const filterRelayResponse = Effect.fn("cloud.filter_relay_response")(function* (
54 response: HttpClientResponse.HttpClientResponse,
55) {
56 if (response.status >= 200 && response.status < 300) return response;
57 const decoded = yield* HttpClientResponse.schemaBodyJson(RelayProtectedError)(response).pipe(
58 Effect.option,
59 );
60 const ray = response.headers["cf-ray"];
61 const requestId = ray && /^[a-zA-Z0-9-]{1,128}$/.test(ray) ? ` Cloudflare Ray ID: ${ray}.` : "";
62 const description = Option.isSome(decoded)
63 ? `T3 Connect: ${decoded.value.message}. ${recoveryHint(decoded.value)} Trace ID: ${decoded.value.traceId}.`
64 : `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}`;
65
66 if (response.status === 401) {
67 return yield* new RelayRequestError({ rejection: "unauthorized", description });
68 }
69 if (response.status === 403) {
70 return yield* new RelayRequestError({ rejection: "forbidden", description });
71 }
72 if (
73 response.status >= 400 &&
74 response.status < 500 &&
75 response.status !== 408 &&
76 response.status !== 429
77 ) {
78 return yield* new RelayRequestError({ rejection: "rejected", description });
79 }
80 return yield* new RelayRequestError({ rejection: "unavailable", description });
81});