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

errors.ts

168 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 case "RelayEnvironmentLinkLimitExceededError":
49 return new ConnectionBlockedError({
50 reason: "permission",
51 detail: error.message,
52 traceId: error.traceId,
53 });
54 case "RelayEnvironmentEndpointTimedOutError":
55 return new ConnectionTransientError({
56 reason: "timeout",
57 detail: error.message,
58 traceId: error.traceId,
59 });
60 case "RelayEnvironmentEndpointUnavailableError":
61 case "RelayEnvironmentLinkUnavailableError":
62 return new ConnectionTransientError({
63 reason: "endpoint-unavailable",
64 detail: error.message,
65 traceId: error.traceId,
66 });
67 case "RelayEnvironmentLinkFailedError":
68 case "RelayInternalError":
69 return new ConnectionTransientError({
70 reason: "relay-unavailable",
71 detail: error.message,
72 traceId: error.traceId,
73 });
74 }
76​
77export function mapManagedRelayError(error: ManagedRelayClientError): ConnectionAttemptError {
78 switch (error._tag) {
79 case "ManagedRelayRequestFailedError":
80 if (error.relayError) {
81 return relayProtectedError(error.relayError);
82 }
83 return new ConnectionTransientError({
84 reason: "relay-unavailable",
85 detail: error.message,
86 ...(error.traceId ? { traceId: error.traceId } : {}),
87 });
88 case "ManagedRelayRequestTimeoutError":
89 return new ConnectionTransientError({
90 reason: "timeout",
91 detail: error.message,
92 });
93 case "ManagedRelayUrlInvalidError":
94 return new ConnectionBlockedError({
95 reason: "configuration",
96 detail: error.message,
97 });
98 case "ManagedRelayAccessTokenScopesUnexpectedError":
99 return new ConnectionBlockedError({
100 reason: "permission",
101 detail: error.message,
102 });
103 case "ManagedRelayDpopKeyLoadError":
104 case "ManagedRelayTokenProofCreationError":
105 case "ManagedRelayRequestProofCreationError":
106 return new ConnectionBlockedError({
107 reason: "authentication",
108 detail: error.message,
109 });
110 }
112​
113export function mapRemoteEnvironmentError(
114 error: RemoteEnvironmentAuthError,
115): ConnectionAttemptError {
116 switch (error._tag) {
117 case "EnvironmentAuthInvalidError":
118 return new ConnectionBlockedError({
119 reason: "authentication",
120 detail: "The environment credential is invalid.",
121 traceId: error.traceId,
122 });
123 case "EnvironmentScopeRequiredError":
124 case "EnvironmentOperationForbiddenError":
125 return new ConnectionBlockedError({
126 reason: "permission",
127 detail: "The environment credential does not grant the required access.",
128 traceId: error.traceId,
129 });
130 case "EnvironmentRequestInvalidError":
131 return new ConnectionBlockedError({
132 reason: "configuration",
133 detail: "The environment rejected the authentication request.",
134 traceId: error.traceId,
135 });
136 case "EnvironmentResourceNotFoundError":
137 // Not expected during connection authorization, but the shared request
138 // error type now includes it (used by resource fetches like the thread
139 // snapshot). Treat it as a configuration issue with the endpoint.
140 return new ConnectionBlockedError({
141 reason: "configuration",
142 detail: "The environment endpoint could not be found.",
143 traceId: error.traceId,
144 });
145 case "RemoteEnvironmentAuthTimeoutError":
146 return new ConnectionTransientError({
147 reason: "timeout",
148 detail: error.message,
149 });
150 case "RemoteEnvironmentAuthFetchError":
151 return new ConnectionTransientError({
152 reason: "network",
153 detail: error.message,
154 });
155 case "EnvironmentInternalError":
156 return new ConnectionTransientError({
157 reason: "remote-unavailable",
158 detail: "The environment could not authorize the connection.",
159 traceId: error.traceId,
160 });
161 case "RemoteEnvironmentAuthInvalidJsonError":
162 case "RemoteEnvironmentAuthUndeclaredStatusError":
163 return new ConnectionTransientError({
164 reason: "remote-unavailable",
165 detail: error.message,
166 });
167 }