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 return error.reason
47 ? `Relay rejected the environment connection request (${error.reason}).`
48 : "Relay rejected the environment connection request.";
49 case "RelayEnvironmentEndpointUnavailableError":
50 return `Relay could not reach the environment endpoint (${error.reason}).`;
51 case "RelayEnvironmentEndpointTimedOutError":
52 return "Relay timed out while contacting the environment endpoint.";
53 case "RelayEnvironmentLinkFailedError":
54 return `Relay could not link the environment (${error.reason}).`;
55 case "RelayEnvironmentLinkUnavailableError":
56 return `Relay cannot provision the managed endpoint (${error.reason}).`;
57 case "RelayEnvironmentLinkLimitExceededError":
58 return `Relay refused the link: this account already has its maximum of ${error.maxTunnels} managed tunnels. Unlink an environment to free one up.`;
59 case "RelayAgentActivityPublishProofExpiredError":
60 return "Relay rejected an expired agent activity publish proof.";
61 case "RelayAgentActivityPublishProofInvalidError":
62 return `Relay rejected the agent activity publish proof (${error.reason}).`;
63 case "RelayInternalError":
64 return `Relay encountered an internal error (${error.reason}).`;
65 }
66}
67
68// A host with a current build gets a new tunnel on its own within minutes of
69// coming back, which clears this reason. While it is still reported, the host
70// is either still off or running a build too old to do that.
71export const RELAY_TUNNEL_RELEASED_MESSAGE =
72 "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.";
73
74/** User-facing text for an offline status, or null when the relay gave no known reason. */
75export function relayOfflineReasonMessage(
76 status: Pick<RelayEnvironmentStatusResponse, "offlineReason">,
77): string | null {
78 switch (status.offlineReason) {
79 case "tunnel_released":
80 return RELAY_TUNNEL_RELEASED_MESSAGE;
81 case undefined:
82 return null;
83 }
84}