Open History to see the last commit
1import type { ClientConnectionMethod, EnvironmentId } from "@t3tools/contracts";
2import type { RelayProtectedError } from "@t3tools/contracts/relay";
3import type { ManagedRelayClientError } from "../relay/managedRelay.ts";
4import { dpopFailureMessage, relayProtectedErrorMessage } from "../relay/errorPresentation.ts";
5import type { RemoteEnvironmentAuthError } from "../authorization/remote.ts";
6import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
7import {
8 ConnectionBlockedError,
9 type ConnectionAttemptError,
10 ConnectionTransientError,
11} from "./model.ts";
12
13export function profileMissingError(connectionId: string): ConnectionBlockedError {
14 return new ConnectionBlockedError({
15 reason: "configuration",
16 detail: `Connection profile ${connectionId} is unavailable.`,
17 });
18}
19
20export function credentialMissingError(connectionId: string): ConnectionBlockedError {
21 return new ConnectionBlockedError({
22 reason: "authentication",
23 detail: `Connection credential ${connectionId} is unavailable.`,
24 });
25}
26
27export function environmentMismatchError(input: {
28 readonly expected: EnvironmentId;
29 readonly actual: EnvironmentId;
30}): ConnectionBlockedError {
31 return new ConnectionBlockedError({
32 reason: "configuration",
33 detail: `Connected environment ${input.actual} does not match ${input.expected}.`,
34 });
35}
36
37function relayProtectedError(error: RelayProtectedError): ConnectionAttemptError {
38 switch (error._tag) {
39 case "RelayAuthInvalidError":
40 case "RelayEnvironmentLinkProofExpiredError":
41 case "RelayAgentActivityPublishProofExpiredError":
42 case "RelayAgentActivityPublishProofInvalidError":
43 return new ConnectionBlockedError({
44 reason: "authentication",
45 detail: relayProtectedErrorMessage(error),
46 traceId: error.traceId,
47 });
48 case "RelayEnvironmentConnectNotAuthorizedError":
49 case "RelayEnvironmentLinkProofInvalidError":
50 case "RelayEnvironmentLinkLimitExceededError":
51 return new ConnectionBlockedError({
52 reason: "permission",
53 detail: relayProtectedErrorMessage(error),
54 traceId: error.traceId,
55 });
56 case "RelayEnvironmentEndpointTimedOutError":
57 return new ConnectionTransientError({
58 reason: "timeout",
59 detail: relayProtectedErrorMessage(error),
60 traceId: error.traceId,
61 });
62 case "RelayEnvironmentEndpointUnavailableError":
63 case "RelayEnvironmentLinkUnavailableError":
64 return new ConnectionTransientError({
65 reason: "endpoint-unavailable",
66 detail: relayProtectedErrorMessage(error),
67 traceId: error.traceId,
68 });
69 case "RelayEnvironmentLinkFailedError":
70 case "RelayInternalError":
71 return new ConnectionTransientError({
72 reason: "relay-unavailable",
73 detail: relayProtectedErrorMessage(error),
74 traceId: error.traceId,
75 });
76 }
77}
78
79export function mapManagedRelayError(error: ManagedRelayClientError): ConnectionAttemptError {
80 switch (error._tag) {
81 case "ManagedRelayRequestFailedError":
82 if (error.relayError) {
83 return relayProtectedError(error.relayError);
84 }
85 return new ConnectionTransientError({
86 reason: "relay-unavailable",
87 detail: error.message,
88 ...(error.traceId ? { traceId: error.traceId } : {}),
89 });
90 case "ManagedRelayRequestTimeoutError":
91 return new ConnectionTransientError({
92 reason: "timeout",
93 detail: error.message,
94 });
95 case "ManagedRelayUrlInvalidError":
96 return new ConnectionBlockedError({
97 reason: "configuration",
98 detail: error.message,
99 });
100 case "ManagedRelayAccessTokenScopesUnexpectedError":
101 return new ConnectionBlockedError({
102 reason: "permission",
103 detail: error.message,
104 });
105 case "ManagedRelayDpopKeyLoadError":
106 case "ManagedRelayTokenProofCreationError":
107 case "ManagedRelayRequestProofCreationError":
108 return new ConnectionBlockedError({
109 reason: "authentication",
110 detail: error.message,
111 });
112 }
113}
114
115export function mapRemoteEnvironmentError(
116 error: RemoteEnvironmentAuthError,
117 connectionMethod: ClientConnectionMethod = "direct",
118): ConnectionAttemptError {
119 const networkHint = connectionMethod === "relay" ? ` ${NETWORK_BLOCKING_HINT}` : "";
120 switch (error._tag) {
121 case "EnvironmentAuthInvalidError":
122 return new ConnectionBlockedError({
123 reason: "authentication",
124 detail: "The environment credential is invalid.",
125 traceId: error.traceId,
126 });
127 case "EnvironmentScopeRequiredError":
128 case "EnvironmentOperationForbiddenError":
129 return new ConnectionBlockedError({
130 reason: "permission",
131 detail: "The environment credential does not grant the required access.",
132 traceId: error.traceId,
133 });
134 case "EnvironmentRequestInvalidError":
135 return new ConnectionBlockedError({
136 reason: "configuration",
137 detail: "The environment rejected the authentication request.",
138 traceId: error.traceId,
139 });
140 case "EnvironmentResourceNotFoundError":
141 // Not expected during connection authorization, but the shared request
142 // error type now includes it (used by resource fetches like the thread
143 // snapshot). Treat it as a configuration issue with the endpoint.
144 return new ConnectionBlockedError({
145 reason: "configuration",
146 detail: "The environment endpoint could not be found.",
147 traceId: error.traceId,
148 });
149 case "RemoteEnvironmentAuthTimeoutError":
150 return new ConnectionTransientError({
151 reason: "timeout",
152 detail: `${error.message}${networkHint}`,
153 });
154 case "RemoteEnvironmentAuthFetchError":
155 return new ConnectionTransientError({
156 reason: "network",
157 detail: `${error.message}${networkHint}`,
158 });
159 case "EnvironmentInternalError":
160 return new ConnectionTransientError({
161 reason: "remote-unavailable",
162 detail: "The environment could not authorize the connection.",
163 traceId: error.traceId,
164 });
165 case "RemoteEnvironmentAuthInvalidJsonError":
166 case "RemoteEnvironmentAuthUndeclaredStatusError":
167 return new ConnectionTransientError({
168 reason: "remote-unavailable",
169 detail: error.message,
170 });
171 }
172}
173
174/**
175 * Map an environment error from a request that used DPoP authentication. An
176 * older environment server reports a DPoP clock failure as the same generic
177 * invalid-credential response as other failures, so keep the compatibility
178 * hint cautious when the server omits the category. Newer servers can identify
179 * clock and non-clock proof failures precisely.
180 */
181export function mapRemoteDpopEnvironmentError(
182 error: RemoteEnvironmentAuthError,
183): ConnectionAttemptError {
184 if (error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential") {
185 return new ConnectionBlockedError({
186 reason: "authentication",
187 detail: dpopFailureMessage("The environment credential is invalid.", error.dpopFailureReason),
188 traceId: error.traceId,
189 });
190 }
191 return mapRemoteEnvironmentError(error, "relay");
192}