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

errors.test.ts

107 lines3.7 KB

1import {
2 RelayAuthInvalidError,
3 RelayEnvironmentConnectNotAuthorizedError,
4 RelayEnvironmentEndpointTimedOutError,
5} from "@t3tools/contracts/relay";
6import { describe, expect, it } from "@effect/vitest";
7​
8import { mapManagedRelayError, mapRemoteDpopEnvironmentError } from "./errors.ts";
9import { DPOP_UNKNOWN_HINT } from "../relay/errorPresentation.ts";
10import { ManagedRelayRequestFailedError } from "../relay/managedRelay.ts";
11import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
12import { RemoteEnvironmentAuthFetchError, RemoteEnvironmentAuthTimeoutError } from "../rpc/http.ts";
13​
14describe("mapManagedRelayError", () => {
15 it("keeps a timeout reported by the relay distinct from a local network failure", () => {
16 const relayError = new RelayEnvironmentEndpointTimedOutError({
17 code: "environment_endpoint_timed_out",
18 traceId: "trace-server-timeout",
19 });
20 const mapped = mapManagedRelayError(
21 new ManagedRelayRequestFailedError({
22 action: "connect relay environment",
23 cause: relayError,
24 relayError,
25 }),
26 );
27 expect(mapped).toMatchObject({
28 reason: "timeout",
29 detail: "Relay timed out while contacting the environment endpoint.",
30 traceId: "trace-server-timeout",
31 });
32 });
33​
34 it("retries a tunnel that is still being set up, and blocks other refusals", () => {
35 const mapConnectRefusal = (reason: RelayEnvironmentConnectNotAuthorizedError["reason"]) => {
36 const relayError = new RelayEnvironmentConnectNotAuthorizedError({
37 code: "environment_connect_not_authorized",
38 reason,
39 traceId: "trace-connect-refused",
40 });
41 return mapManagedRelayError(
42 new ManagedRelayRequestFailedError({
43 action: "connect relay environment",
44 cause: relayError,
45 relayError,
46 }),
47 );
48 };
49​
50 expect(mapConnectRefusal("managed_endpoint_allocation_not_ready")).toMatchObject({
51 _tag: "ConnectionTransientError",
52 reason: "endpoint-unavailable",
53 detail: "The environment's T3 Connect tunnel is still being set up.",
54 traceId: "trace-connect-refused",
55 });
56 expect(mapConnectRefusal("endpoint_provider_not_managed")).toMatchObject({
57 _tag: "ConnectionBlockedError",
58 reason: "permission",
59 detail: expect.stringContaining("turn T3 Connect on"),
60 });
61 expect(mapConnectRefusal(undefined)).toMatchObject({
62 _tag: "ConnectionBlockedError",
63 reason: "permission",
64 });
65 });
66​
67 it("presents clock skew as one possible cause for a generic DPoP error", () => {
68 const mapped = mapManagedRelayError(
69 new ManagedRelayRequestFailedError({
70 action: "connect relay environment",
71 cause: new Error("request failed"),
72 relayError: new RelayAuthInvalidError({
73 code: "auth_invalid",
74 reason: "invalid_dpop",
75 traceId: "trace-1",
76 }),
77 traceId: "trace-1",
78 }),
79 );
80​
81 expect(mapped).toMatchObject({
82 _tag: "ConnectionBlockedError",
83 reason: "authentication",
84 detail: `Relay rejected the DPoP proof. ${DPOP_UNKNOWN_HINT}`,
85 traceId: "trace-1",
86 });
87 });
88});
89​
90describe("mapRemoteDpopEnvironmentError", () => {
91 it.each([
92 new RemoteEnvironmentAuthFetchError({
93 message: "Failed to fetch remote environment endpoint.",
94 cause: new TypeError("Failed to fetch"),
95 }),
96 new RemoteEnvironmentAuthTimeoutError({
97 requestUrl: "https://environment.example.test",
98 timeoutMs: 10_000,
99 }),
100 ])("suggests another network when the relay endpoint cannot be reached: $_tag", (error) => {
101 const mapped = mapRemoteDpopEnvironmentError(error);
102 expect(mapped).toMatchObject({
103 _tag: "ConnectionTransientError",
104 detail: `${error.message} ${NETWORK_BLOCKING_HINT}`,
105 });
106 });
107});