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

errors.ts

201 lines7.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 });
19​
20export function credentialMissingError(connectionId: string): ConnectionBlockedError {
21 return new ConnectionBlockedError({
22 reason: "authentication",
23 detail: `Connection credential ${connectionId} is unavailable.`,
24 });
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 });
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 // A host swapping in a new tunnel leaves its allocation unready until
50 // the tunnel is up, and nothing on the client changes when it is.
51 if (error.reason === "managed_endpoint_allocation_not_ready") {
52 return new ConnectionTransientError({
53 reason: "endpoint-unavailable",
54 detail: relayProtectedErrorMessage(error),
55 traceId: error.traceId,
56 });
57 }
58 return new ConnectionBlockedError({
59 reason: "permission",
60 detail: relayProtectedErrorMessage(error),
61 traceId: error.traceId,
62 });
63 case "RelayEnvironmentLinkProofInvalidError":
64 case "RelayEnvironmentLinkLimitExceededError":
65 return new ConnectionBlockedError({
66 reason: "permission",
67 detail: relayProtectedErrorMessage(error),
68 traceId: error.traceId,
69 });
70 case "RelayEnvironmentEndpointTimedOutError":
71 return new ConnectionTransientError({
72 reason: "timeout",
73 detail: relayProtectedErrorMessage(error),
74 traceId: error.traceId,
75 });
76 case "RelayEnvironmentEndpointUnavailableError":
77 case "RelayEnvironmentLinkUnavailableError":
78 return new ConnectionTransientError({
79 reason: "endpoint-unavailable",
80 detail: relayProtectedErrorMessage(error),
81 traceId: error.traceId,
82 });
83 case "RelayEnvironmentLinkFailedError":
84 case "RelayInternalError":
85 return new ConnectionTransientError({
86 reason: "relay-unavailable",
87 detail: relayProtectedErrorMessage(error),
88 traceId: error.traceId,
89 });
90 }
92​
93export function mapManagedRelayError(error: ManagedRelayClientError): ConnectionAttemptError {
94 switch (error._tag) {
95 case "ManagedRelayRequestFailedError":
96 if (error.relayError) {
97 return relayProtectedError(error.relayError);
98 }
99 return new ConnectionTransientError({
100 reason: "relay-unavailable",
101 detail: error.message,
102 ...(error.traceId ? { traceId: error.traceId } : {}),
103 });
104 case "ManagedRelayRequestTimeoutError":
105 return new ConnectionTransientError({
106 reason: "timeout",
107 detail: error.message,
108 });
109 case "ManagedRelayUrlInvalidError":
110 return new ConnectionBlockedError({
111 reason: "configuration",
112 detail: error.message,
113 });
114 case "ManagedRelayAccessTokenScopesUnexpectedError":
115 return new ConnectionBlockedError({
116 reason: "permission",
117 detail: error.message,
118 });
119 case "ManagedRelayDpopKeyLoadError":
120 case "ManagedRelayTokenProofCreationError":
121 case "ManagedRelayRequestProofCreationError":
122 return new ConnectionBlockedError({
123 reason: "authentication",
124 detail: error.message,
125 });
126 }
128​
129export function mapRemoteEnvironmentError(
130 error: RemoteEnvironmentAuthError,
131 connectionMethod: ClientConnectionMethod = "direct",
132): ConnectionAttemptError {
133 const networkHint = connectionMethod === "relay" ? ` ${NETWORK_BLOCKING_HINT}` : "";
134 switch (error._tag) {
135 case "EnvironmentAuthInvalidError":
136 return new ConnectionBlockedError({
137 reason: "authentication",
138 detail: "The environment credential is invalid.",
139 traceId: error.traceId,
140 });
141 case "EnvironmentScopeRequiredError":
142 case "EnvironmentOperationForbiddenError":
143 return new ConnectionBlockedError({
144 reason: "permission",
145 detail: "The environment credential does not grant the required access.",
146 traceId: error.traceId,
147 });
148 case "EnvironmentRequestInvalidError":
149 return new ConnectionBlockedError({
150 reason: "configuration",
151 detail: "The environment rejected the authentication request.",
152 traceId: error.traceId,
153 });
154 case "EnvironmentResourceNotFoundError":
155 return new ConnectionBlockedError({
156 reason: "configuration",
157 detail: "The environment endpoint could not be found.",
158 traceId: error.traceId,
159 });
160 case "RemoteEnvironmentAuthTimeoutError":
161 return new ConnectionTransientError({
162 reason: "timeout",
163 detail: `${error.message}${networkHint}`,
164 });
165 case "RemoteEnvironmentAuthFetchError":
166 return new ConnectionTransientError({
167 reason: "network",
168 detail: `${error.message}${networkHint}`,
169 });
170 case "EnvironmentInternalError":
171 return new ConnectionTransientError({
172 reason: "remote-unavailable",
173 detail: "The environment could not authorize the connection.",
174 traceId: error.traceId,
175 });
176 case "RemoteEnvironmentAuthInvalidJsonError":
177 case "RemoteEnvironmentAuthUndeclaredStatusError":
178 return new ConnectionTransientError({
179 reason: "remote-unavailable",
180 detail: error.message,
181 });
182 }
184​
185/**
186 * An older environment server reports a DPoP clock failure as the same generic
187 * invalid-credential response as other failures, so keep the compatibility hint
188 * cautious when the server omits the category.
189 */
190export function mapRemoteDpopEnvironmentError(
191 error: RemoteEnvironmentAuthError,
192): ConnectionAttemptError {
193 if (error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential") {
194 return new ConnectionBlockedError({
195 reason: "authentication",
196 detail: dpopFailureMessage("The environment credential is invalid.", error.dpopFailureReason),
197 traceId: error.traceId,
198 });
199 }
200 return mapRemoteEnvironmentError(error, "relay");