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