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

relayResponse.ts

92 lines4.1 KB

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 }
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 });
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 }
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 // The only 401 that clears itself: a proof signed with a skewed clock (a host
67 // that booted before NTP) expires on arrival, and the next attempt signs a new
68 // one. It is recognized only from a schema-decoded relay body. Every other
69 // 401, including a revoked bearer or an undecodable body, stays permanent.
70 if (
71 response.status === 401 &&
72 Option.isSome(decoded) &&
73 decoded.value._tag === "RelayEnvironmentLinkProofExpiredError"
74 ) {
75 return yield* new RelayRequestError({ rejection: "unavailable", description });
76 }
77 if (response.status === 401) {
78 return yield* new RelayRequestError({ rejection: "unauthorized", description });
79 }
80 if (response.status === 403) {
81 return yield* new RelayRequestError({ rejection: "forbidden", description });
82 }
83 if (
84 response.status >= 400 &&
85 response.status < 500 &&
86 response.status !== 408 &&
87 response.status !== 429
88 ) {
89 return yield* new RelayRequestError({ rejection: "rejected", description });
90 }
91 return yield* new RelayRequestError({ rejection: "unavailable", description });
92});