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

errors.ts

167 lines5.7 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import type { RelayProtectedError } from "@t3tools/contracts/relay";
3import type { ManagedRelayClientError } from "../relay/managedRelay.ts";
4import type { RemoteEnvironmentAuthError } from "../authorization/remote.ts";
5import {
6 ConnectionBlockedError,
7 type ConnectionAttemptError,
8 ConnectionTransientError,
9} from "./model.ts";
10​
11export function profileMissingError(connectionId: string): ConnectionBlockedError {
12 return new ConnectionBlockedError({
13 reason: "configuration",
14 detail: `Connection profile ${connectionId} is unavailable.`,
15 });
17​
18export function credentialMissingError(connectionId: string): ConnectionBlockedError {
19 return new ConnectionBlockedError({
20 reason: "authentication",
21 detail: `Connection credential ${connectionId} is unavailable.`,
22 });
24​
25export function environmentMismatchError(input: {
26 readonly expected: EnvironmentId;
27 readonly actual: EnvironmentId;
28}): ConnectionBlockedError {
29 return new ConnectionBlockedError({
30 reason: "configuration",
31 detail: `Connected environment ${input.actual} does not match ${input.expected}.`,
32 });
34​
35function relayProtectedError(error: RelayProtectedError): ConnectionAttemptError {
36 switch (error._tag) {
37 case "RelayAuthInvalidError":
38 case "RelayEnvironmentLinkProofExpiredError":
39 case "RelayAgentActivityPublishProofExpiredError":
40 case "RelayAgentActivityPublishProofInvalidError":
41 return new ConnectionBlockedError({
42 reason: "authentication",
43 detail: error.message,
44 traceId: error.traceId,
45 });
46 case "RelayEnvironmentConnectNotAuthorizedError":
47 case "RelayEnvironmentLinkProofInvalidError":
48 return new ConnectionBlockedError({
49 reason: "permission",
50 detail: error.message,
51 traceId: error.traceId,
52 });
53 case "RelayEnvironmentEndpointTimedOutError":
54 return new ConnectionTransientError({
55 reason: "timeout",
56 detail: error.message,
57 traceId: error.traceId,
58 });
59 case "RelayEnvironmentEndpointUnavailableError":
60 case "RelayEnvironmentLinkUnavailableError":
61 return new ConnectionTransientError({
62 reason: "endpoint-unavailable",
63 detail: error.message,
64 traceId: error.traceId,
65 });
66 case "RelayEnvironmentLinkFailedError":
67 case "RelayInternalError":
68 return new ConnectionTransientError({
69 reason: "relay-unavailable",
70 detail: error.message,
71 traceId: error.traceId,
72 });
73 }
75​
76export function mapManagedRelayError(error: ManagedRelayClientError): ConnectionAttemptError {
77 switch (error._tag) {
78 case "ManagedRelayRequestFailedError":
79 if (error.relayError) {
80 return relayProtectedError(error.relayError);
81 }
82 return new ConnectionTransientError({
83 reason: "relay-unavailable",
84 detail: error.message,
85 ...(error.traceId ? { traceId: error.traceId } : {}),
86 });
87 case "ManagedRelayRequestTimeoutError":
88 return new ConnectionTransientError({
89 reason: "timeout",
90 detail: error.message,
91 });
92 case "ManagedRelayUrlInvalidError":
93 return new ConnectionBlockedError({
94 reason: "configuration",
95 detail: error.message,
96 });
97 case "ManagedRelayAccessTokenScopesUnexpectedError":
98 return new ConnectionBlockedError({
99 reason: "permission",
100 detail: error.message,
101 });
102 case "ManagedRelayDpopKeyLoadError":
103 case "ManagedRelayTokenProofCreationError":
104 case "ManagedRelayRequestProofCreationError":
105 return new ConnectionBlockedError({
106 reason: "authentication",
107 detail: error.message,
108 });
109 }
111​
112export function mapRemoteEnvironmentError(
113 error: RemoteEnvironmentAuthError,
114): ConnectionAttemptError {
115 switch (error._tag) {
116 case "EnvironmentAuthInvalidError":
117 return new ConnectionBlockedError({
118 reason: "authentication",
119 detail: "The environment credential is invalid.",
120 traceId: error.traceId,
121 });
122 case "EnvironmentScopeRequiredError":
123 case "EnvironmentOperationForbiddenError":
124 return new ConnectionBlockedError({
125 reason: "permission",
126 detail: "The environment credential does not grant the required access.",
127 traceId: error.traceId,
128 });
129 case "EnvironmentRequestInvalidError":
130 return new ConnectionBlockedError({
131 reason: "configuration",
132 detail: "The environment rejected the authentication request.",
133 traceId: error.traceId,
134 });
135 case "EnvironmentResourceNotFoundError":
136 // Not expected during connection authorization, but the shared request
137 // error type now includes it (used by resource fetches like the thread
138 // snapshot). Treat it as a configuration issue with the endpoint.
139 return new ConnectionBlockedError({
140 reason: "configuration",
141 detail: "The environment endpoint could not be found.",
142 traceId: error.traceId,
143 });
144 case "RemoteEnvironmentAuthTimeoutError":
145 return new ConnectionTransientError({
146 reason: "timeout",
147 detail: error.message,
148 });
149 case "RemoteEnvironmentAuthFetchError":
150 return new ConnectionTransientError({
151 reason: "network",
152 detail: error.message,
153 });
154 case "EnvironmentInternalError":
155 return new ConnectionTransientError({
156 reason: "remote-unavailable",
157 detail: "The environment could not authorize the connection.",
158 traceId: error.traceId,
159 });
160 case "RemoteEnvironmentAuthInvalidJsonError":
161 case "RemoteEnvironmentAuthUndeclaredStatusError":
162 return new ConnectionTransientError({
163 reason: "remote-unavailable",
164 detail: error.message,
165 });
166 }