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

errors.ts

295 lines10.2 KB

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