Open History to see the last commit
1import type { DpopFailureReason } from "@t3tools/contracts";
2import type { RelayEnvironmentStatusResponse, RelayProtectedError } from "@t3tools/contracts/relay";
3
4export const DPOP_CLOCK_HINT =
5 "Hint: Check that automatic date and time is enabled on both devices, then try again.";
6
7/** Older servers omit the DPoP category, but newer servers can also omit it for
8 * a credential failure that happens after proof verification. */
9export const DPOP_UNKNOWN_HINT =
10 "Hint: Try again. If it still fails, clock skew may be the cause; check that automatic date and time is enabled on both devices.";
11
12export const DPOP_RETRY_HINT = "Hint: Try again. If the problem continues, copy the trace ID.";
13
14function dpopFailureHint(reason: DpopFailureReason | undefined): string {
15 if (reason === "time_window") return DPOP_CLOCK_HINT;
16 if (reason === undefined) return DPOP_UNKNOWN_HINT;
17 return DPOP_RETRY_HINT;
18}
19
20export function dpopFailureMessage(message: string, reason: DpopFailureReason | undefined): string {
21 return `${message} ${dpopFailureHint(reason)}`;
22}
23
24export function relayProtectedErrorMessage(error: RelayProtectedError): string {
25 switch (error._tag) {
26 case "RelayAuthInvalidError":
27 switch (error.reason) {
28 case "missing_bearer":
29 case "invalid_bearer":
30 return "Relay rejected the cloud session token.";
31 case "invalid_dpop":
32 return dpopFailureMessage("Relay rejected the DPoP proof.", error.dpopFailureReason);
33 case "not_authorized":
34 return "Relay rejected the authenticated request.";
35 }
36 case "RelayEnvironmentLinkProofExpiredError":
37 return "Relay rejected an expired environment link proof.";
38 case "RelayEnvironmentLinkProofInvalidError":
39 return `Relay rejected the environment link proof (${error.reason}).`;
40 case "RelayEnvironmentConnectNotAuthorizedError":
41 // "Not authorized" covers non-auth causes too; surface the reason so a
42 // missing link does not read as a credential problem.
43 if (error.reason === "environment_link_not_found") {
44 return "Relay has no active link for this environment. The environment server may not have re-established its link yet.";
45 }
46 if (error.reason === "managed_endpoint_allocation_not_ready") {
47 return "The environment's T3 Connect tunnel is still being set up.";
48 }
49 return error.reason
50 ? `Relay rejected the environment connection request (${error.reason}).`
51 : "Relay rejected the environment connection request.";
52 case "RelayEnvironmentEndpointUnavailableError":
53 return `Relay could not reach the environment endpoint (${error.reason}).`;
54 case "RelayEnvironmentEndpointTimedOutError":
55 return "Relay timed out while contacting the environment endpoint.";
56 case "RelayEnvironmentLinkFailedError":
57 return `Relay could not link the environment (${error.reason}).`;
58 case "RelayEnvironmentLinkUnavailableError":
59 return `Relay cannot provision the managed endpoint (${error.reason}).`;
60 case "RelayEnvironmentLinkLimitExceededError":
61 return `Relay refused the link: this account already has its maximum of ${error.maxTunnels} managed tunnels. Unlink an environment to free one up.`;
62 case "RelayAgentActivityPublishProofExpiredError":
63 return "Relay rejected an expired agent activity publish proof.";
64 case "RelayAgentActivityPublishProofInvalidError":
65 return `Relay rejected the agent activity publish proof (${error.reason}).`;
66 case "RelayInternalError":
67 return `Relay encountered an internal error (${error.reason}).`;
68 }
69}
70
71// A host with a current build gets a new tunnel on its own within minutes of
72// coming back, which clears this reason. While it is still reported, the host
73// is either still off or running a build too old to do that.
74export const RELAY_TUNNEL_RELEASED_MESSAGE =
75 "Offline for a while, so its T3 Connect tunnel was removed. Start T3 Code on that computer and update it to the latest version to reconnect.";
76
77/** User-facing text for an offline status, or null when the relay gave no known reason. */
78export function relayOfflineReasonMessage(
79 status: Pick<RelayEnvironmentStatusResponse, "offlineReason">,
80): string | null {
81 switch (status.offlineReason) {
82 case "tunnel_released":
83 return RELAY_TUNNEL_RELEASED_MESSAGE;
84 case undefined:
85 return null;
86 }
87}