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

errors.ts

332 lines11.3 KB

1import type { ClientConnectionMethod, EnvironmentId } from "@t3tools/contracts";
2import type { RelayProtectedError } from "@t3tools/contracts/relay";
3import type { ManagedRelayClientError } from "../relay/managedRelay.ts";
4import { msg } from "gt-i18n";
5import { dpopFailureText, relayProtectedErrorText } from "../relay/errorPresentation.ts";
6import type { RemoteEnvironmentAuthError } from "../authorization/remote.ts";
7import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
8import {
9 detailFromError,
10 errorI18nMessage,
11 markedTextEnglish,
12 translatableDetail,
13} from "../errors/translatable.ts";
14import {
15 ConnectionBlockedError,
16 type ConnectionAttemptError,
17 ConnectionTransientError,
18} from "./model.ts";
19​
20export function profileMissingError(connectionId: string): ConnectionBlockedError {
21 return new ConnectionBlockedError({
22 reason: "configuration",
23 ...translatableDetail(msg`Connection profile ${{ connectionId }} is unavailable.`),
24 hop: "device",
25 });
27​
28export function credentialMissingError(connectionId: string): ConnectionBlockedError {
29 return new ConnectionBlockedError({
30 reason: "authentication",
31 ...translatableDetail(msg`Connection credential ${{ connectionId }} is unavailable.`),
32 hop: "device",
33 });
35​
36/** The host itself answered and refused this device's credential, which pairing again fixes. */
37export function isHostCredentialRefusal(error: ConnectionAttemptError): boolean {
38 return (
39 error._tag === "ConnectionBlockedError" &&
40 error.hop === "server" &&
41 (error.reason === "authentication" || error.reason === "permission") &&
42 error.failureCause !== "clock_skew"
43 );
45​
46export function environmentMismatchError(input: {
47 readonly expected: EnvironmentId;
48 readonly actual: EnvironmentId;
49}): ConnectionBlockedError {
50 return new ConnectionBlockedError({
51 reason: "configuration",
52 ...translatableDetail(
53 msg`This address now runs a different T3 Code install (${{ actual: input.actual }}, not ${{ expected: input.expected }}). Remove this connection and add it again.`,
54 ),
55 });
57​
58function relayProtectedError(error: RelayProtectedError): ConnectionAttemptError {
59 const text = relayProtectedErrorText(error);
60 const detail = markedTextEnglish(text);
61 const i18nMessage = text;
62 const traceId = error.traceId;
63 switch (error._tag) {
64 case "RelayAuthInvalidError":
65 return new ConnectionBlockedError({
66 reason: "authentication",
67 detail,
68 i18nMessage,
69 traceId,
70 hop: "relay",
71 ...(error.dpopFailureReason === "time_window" ? { failureCause: "clock_skew" } : {}),
72 });
73 case "RelayEnvironmentLinkProofExpiredError":
74 case "RelayAgentActivityPublishProofExpiredError":
75 case "RelayAgentActivityPublishProofInvalidError":
76 return new ConnectionBlockedError({
77 reason: "authentication",
78 detail,
79 i18nMessage,
80 traceId,
81 hop: "relay",
82 });
83 case "RelayEnvironmentConnectNotAuthorizedError":
84 // A host swapping in a new tunnel leaves its allocation unready until
85 // the tunnel is up, and nothing on the client changes when it is.
86 if (error.reason === "managed_endpoint_allocation_not_ready") {
87 return new ConnectionTransientError({
88 reason: "endpoint-unavailable",
89 detail,
90 i18nMessage,
91 traceId,
92 hop: "tunnel",
93 failureCause: "tunnel_pending",
94 });
95 }
96 if (error.reason === "endpoint_provider_not_managed") {
97 return new ConnectionBlockedError({
98 reason: "permission",
99 detail,
100 i18nMessage,
101 traceId,
102 hop: "tunnel",
103 failureCause: "tunnel_not_enabled",
104 });
105 }
106 return new ConnectionBlockedError({
107 reason: "permission",
108 detail,
109 i18nMessage,
110 traceId,
111 hop: "relay",
112 ...(error.reason === "environment_link_not_found" ? { failureCause: "not_linked" } : {}),
113 });
114 case "RelayEnvironmentLinkProofInvalidError":
115 case "RelayEnvironmentLinkLimitExceededError":
116 return new ConnectionBlockedError({
117 reason: "permission",
118 detail,
119 i18nMessage,
120 traceId,
121 hop: "relay",
122 });
123 case "RelayEnvironmentEndpointTimedOutError":
124 return new ConnectionTransientError({
125 reason: "timeout",
126 detail,
127 i18nMessage,
128 traceId,
129 hop: "tunnel",
130 failureCause: "timeout",
131 });
132 case "RelayEnvironmentEndpointUnavailableError": {
133 const endpointFailure =
134 error.endpointFailure ??
135 (error.reason === "endpoint_response_invalid" ? "invalid_response" : undefined);
136 return new ConnectionTransientError({
137 reason: "endpoint-unavailable",
138 detail,
139 i18nMessage,
140 traceId,
141 // The tunnel carried the request when T3 Code is what failed to answer.
142 hop:
143 endpointFailure === "origin_unreachable" || endpointFailure === "invalid_response"
144 ? "server"
145 : "tunnel",
146 via: "relay",
147 ...(endpointFailure ? { failureCause: endpointFailure } : {}),
148 ...(error.endpointStatus ? { httpStatus: error.endpointStatus } : {}),
149 });
150 }
151 case "RelayEnvironmentLinkUnavailableError":
152 return new ConnectionTransientError({
153 reason: "endpoint-unavailable",
154 detail,
155 i18nMessage,
156 traceId,
157 hop: "tunnel",
158 });
159 case "RelayEnvironmentLinkFailedError":
160 case "RelayInternalError":
161 return new ConnectionTransientError({
162 reason: "relay-unavailable",
163 detail,
164 i18nMessage,
165 traceId,
166 hop: "relay",
167 failureCause: "relay_error",
168 });
169 }
171​
172export function mapManagedRelayError(error: ManagedRelayClientError): ConnectionAttemptError {
173 switch (error._tag) {
174 case "ManagedRelayRequestFailedError":
175 if (error.relayError) {
176 return relayProtectedError(error.relayError);
177 }
178 return new ConnectionTransientError({
179 reason: "relay-unavailable",
180 ...detailFromError(error),
181 hop: "relay",
182 // A status means the relay answered, so this device did reach it.
183 ...(error.status !== undefined
184 ? { httpStatus: error.status, failureCause: "relay_error" }
185 : {}),
186 ...(error.traceId ? { traceId: error.traceId } : {}),
187 });
188 case "ManagedRelayRequestTimeoutError":
189 return new ConnectionTransientError({
190 reason: "timeout",
191 ...detailFromError(error),
192 hop: "relay",
193 });
194 case "ManagedRelayUrlInvalidError":
195 return new ConnectionBlockedError({
196 reason: "configuration",
197 ...detailFromError(error),
198 hop: "relay",
199 });
200 case "ManagedRelayAccessTokenScopesUnexpectedError":
201 return new ConnectionBlockedError({
202 reason: "permission",
203 ...detailFromError(error),
204 hop: "relay",
205 });
206 case "ManagedRelayDpopKeyLoadError":
207 case "ManagedRelayTokenProofCreationError":
208 case "ManagedRelayRequestProofCreationError":
209 return new ConnectionBlockedError({
210 reason: "authentication",
211 ...detailFromError(error),
212 hop: "device",
213 });
214 }
216​
217export function mapRemoteEnvironmentError(
218 error: RemoteEnvironmentAuthError,
219 connectionMethod: ClientConnectionMethod = "direct",
220): ConnectionAttemptError {
221 const withNetworkHint = (cause: { readonly message: string }) => {
222 if (connectionMethod !== "relay") return detailFromError(cause);
223 const text = errorI18nMessage(cause);
224 return text === undefined
225 ? { detail: `${cause.message} ${NETWORK_BLOCKING_HINT}` }
226 : translatableDetail(...text, NETWORK_BLOCKING_HINT);
227 };
228 // Over T3 Connect the relay already reached the host, so a request that never
229 // arrives was blocked between this device and the tunnel.
230 const unreachableHop = connectionMethod === "relay" ? "tunnel" : "server";
231 const via = connectionMethod === "relay" ? "relay" : "direct";
232 switch (error._tag) {
233 case "EnvironmentAuthInvalidError":
234 return new ConnectionBlockedError({
235 reason: "authentication",
236 ...translatableDetail(msg("The environment credential is invalid.")),
237 traceId: error.traceId,
238 hop: "server",
239 via,
240 });
241 case "EnvironmentScopeRequiredError":
242 case "EnvironmentOperationForbiddenError":
243 return new ConnectionBlockedError({
244 reason: "permission",
245 ...translatableDetail(
246 msg("The environment credential does not grant the required access."),
247 ),
248 traceId: error.traceId,
249 hop: "server",
250 via,
251 });
252 case "EnvironmentRequestInvalidError":
253 return new ConnectionBlockedError({
254 reason: "configuration",
255 ...translatableDetail(msg("The environment rejected the authentication request.")),
256 traceId: error.traceId,
257 hop: "server",
258 via,
259 });
260 case "EnvironmentResourceNotFoundError":
261 return new ConnectionBlockedError({
262 reason: "configuration",
263 ...translatableDetail(msg("The environment endpoint could not be found.")),
264 traceId: error.traceId,
265 hop: "server",
266 via,
267 });
268 case "RemoteEnvironmentAuthTimeoutError":
269 return new ConnectionTransientError({
270 reason: "timeout",
271 ...withNetworkHint(error),
272 hop: unreachableHop,
273 via,
274 });
275 case "RemoteEnvironmentAuthFetchError":
276 return new ConnectionTransientError({
277 reason: "network",
278 ...withNetworkHint(error),
279 hop: unreachableHop,
280 via,
281 });
282 case "EnvironmentConnectApprovalPendingError":
283 return new ConnectionTransientError({
284 reason: "approval-pending",
285 ...translatableDetail(msg("This device is waiting for approval on the host.")),
286 traceId: error.traceId,
287 hop: "server",
288 via,
289 failureCause: "approval_pending",
290 });
291 case "EnvironmentInternalError":
292 return new ConnectionTransientError({
293 reason: "remote-unavailable",
294 ...translatableDetail(msg("The environment could not authorize the connection.")),
295 traceId: error.traceId,
296 hop: "server",
297 via,
298 });
299 case "RemoteEnvironmentAuthInvalidJsonError":
300 case "RemoteEnvironmentAuthUndeclaredStatusError":
301 return new ConnectionTransientError({
302 reason: "remote-unavailable",
303 ...detailFromError(error),
304 hop: "server",
305 via,
306 });
307 }
309​
310/**
311 * An older environment server reports a DPoP clock failure as the same generic
312 * invalid-credential response as other failures, so keep the compatibility hint
313 * cautious when the server omits the category.
314 */
315export function mapRemoteDpopEnvironmentError(
316 error: RemoteEnvironmentAuthError,
317 connectionMethod: "relay" | "direct" = "relay",
318): ConnectionAttemptError {
319 if (error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential") {
320 return new ConnectionBlockedError({
321 reason: "authentication",
322 ...translatableDetail(
323 ...dpopFailureText(msg("The environment credential is invalid."), error.dpopFailureReason),
324 ),
325 traceId: error.traceId,
326 hop: "server",
327 via: connectionMethod,
328 ...(error.dpopFailureReason === "time_window" ? { failureCause: "clock_skew" } : {}),
329 });
330 }
331 return mapRemoteEnvironmentError(error, connectionMethod);