Skip to content

Commit 75e3f74

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-32873
2 parents 81b2b66 + dff0b11 commit 75e3f74

7 files changed

Lines changed: 136 additions & 39 deletions

File tree

‎apps/web/src/cloud/linkEnvironment.test.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { ManagedRelay } from "@t3tools/client-runtime/relay";
2626
import { layerRemoteHttpClient } from "@t3tools/client-runtime/rpc";
2727
import { __resetDesktopPrimaryAuthForTests } from "../environments/primary/desktopAuth";
2828

29-
import { readCloudLinkState } from "./linkEnvironment";
29+
import { readCloudLinkState, unlinkEnvironmentFromCloud } from "./linkEnvironment";
3030

3131
const TARGET = {
3232
environmentId: "environment-1",
@@ -239,4 +239,49 @@ describe("web cloud link environment client", () => {
239239
}
240240
}),
241241
);
242+
243+
it.effect("reports why the relay kept the environment after a local unlink", () =>
244+
Effect.gen(function* () {
245+
const fetchMock = vi.fn((input: RequestInfo | URL) =>
246+
Promise.resolve(
247+
String(input).startsWith("https://relay.example.test/")
248+
? Response.json(
249+
{
250+
_tag: "RelayInternalError",
251+
code: "internal_error",
252+
reason: "upstream_unavailable",
253+
traceId: "trace-unlink",
254+
},
255+
{ status: 500 },
256+
)
257+
: Response.json({ ok: true, endpointRuntimeStatus: null }),
258+
),
259+
);
260+
vi.stubGlobal("fetch", fetchMock);
261+
vi.stubGlobal("window", {
262+
location: { origin: "t3code://app" },
263+
desktopBridge: {
264+
getLocalEnvironmentBearerToken: vi.fn().mockResolvedValue("desktop-bearer-token"),
265+
} as unknown as DesktopBridge,
266+
});
267+
268+
const error = yield* withServices(
269+
unlinkEnvironmentFromCloud({
270+
target: { environmentId: EnvironmentId.make(TARGET.environmentId) },
271+
clerkToken: "clerk-token",
272+
}),
273+
).pipe(Effect.flip);
274+
275+
expect(fetchMock.mock.calls.map(([input]) => String(input))).toEqual([
276+
"http://127.0.0.1:3000/api/connect/unlink",
277+
"https://relay.example.test/v1/client/environment-links/environment-1",
278+
]);
279+
expect(error).toMatchObject({
280+
_tag: "CloudEnvironmentLinkError",
281+
message:
282+
"Disconnected locally, but T3 Connect did not confirm it removed this environment: Relay encountered an internal error (upstream_unavailable).",
283+
traceId: "trace-unlink",
284+
});
285+
}),
286+
);
242287
});

‎apps/web/src/cloud/linkEnvironment.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ const isEnvironmentCloudApiError = Schema.is(
120120
function decodedRelayClientError(message: string) {
121121
return (cause: ManagedRelay.ManagedRelayClientError) => {
122122
const failed = cause._tag === "ManagedRelayRequestFailedError" ? cause : undefined;
123-
const traceId = failed?.traceId;
124-
const detail = failed ? relayRequestFailureDetail(failed) : null;
123+
const traceId = "traceId" in cause ? cause.traceId : undefined;
124+
const detail = (failed && relayRequestFailureDetail(failed)) ?? cause.message;
125125
return new CloudEnvironmentLinkError({
126126
message: detail ? `${message}: ${detail}` : message,
127127
cause,
@@ -309,10 +309,10 @@ export const unlinkEnvironmentFromCloud: (input: {
309309
environmentId: input.target.environmentId,
310310
})
311311
.pipe(
312-
Effect.catch((cause) =>
313-
Effect.logWarning("Could not revoke cloud environment link after local unlink.", {
314-
cause,
315-
}),
312+
Effect.mapError(
313+
decodedRelayClientError(
314+
"Disconnected locally, but T3 Connect did not confirm it removed this environment",
315+
),
316316
),
317317
);
318318
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ManagedRelay, relayRequestFailureDetail } from "@t3tools/client-runtime/relay";
2+
import * as Schema from "effect/Schema";
3+
4+
const isRequestFailed = Schema.is(ManagedRelay.ManagedRelayRequestFailedError);
5+
const isRequestTimeout = Schema.is(ManagedRelay.ManagedRelayRequestTimeoutError);
6+
7+
/** One short line for an error toast saying why a T3 Connect request failed. */
8+
export function relayFailureReason(cause: unknown): string | null {
9+
for (let current = cause, depth = 0; current instanceof Error && depth < 4; depth += 1) {
10+
if (isRequestTimeout(current)) return "T3 Connect timed out. Try again.";
11+
if (isRequestFailed(current)) {
12+
if (current.transportFailed) return "Could not reach T3 Connect. Check your network.";
13+
if (current.relayError) {
14+
return current.relayError._tag === "RelayInternalError"
15+
? "T3 Connect is unavailable. Try again."
16+
: relayRequestFailureDetail(current);
17+
}
18+
if (current.status === 504) return "T3 Connect timed out. Try again.";
19+
if ((current.status ?? 0) >= 500) return "T3 Connect is unavailable. Try again.";
20+
return relayRequestFailureDetail(current);
21+
}
22+
current = current.cause;
23+
}
24+
return null;
25+
}

‎apps/web/src/cloud/useCloudLinkController.ts‎

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import * as Schema from "effect/Schema";
1818
import { useState } from "react";
1919

2020
import { toastManager } from "../components/ui/toast";
21+
import { stackedThreadToast } from "../components/ui/toastHelpers";
22+
import { relayFailureReason } from "./relayFailureReason";
2123
import { relayEnvironmentDiscovery } from "../state/relay";
2224
import { readEnvironmentScope } from "../state/session";
2325
import { useAtomCommand } from "../state/use-atom-command";
@@ -51,6 +53,7 @@ function isManagedTunnelActive(
5153
}
5254

5355
const isManagedRelayRequestFailedError = Schema.is(ManagedRelay.ManagedRelayRequestFailedError);
56+
const isManagedRelayClientError = Schema.is(ManagedRelay.ManagedRelayClientError);
5457

5558
/** The account's tunnel cap when the relay refused a new tunnel for being at it. */
5659
function tunnelLimitFromLinkFailure(cause: unknown): number | null {
@@ -84,24 +87,28 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) {
8487
const [operationError, setOperationError] = useState<string | null>(null);
8588
const [tunnelLimit, setTunnelLimit] = useState<number | null>(null);
8689

87-
const reportUpdateFailure = (cause: unknown) => {
90+
const reportUpdateFailure = (cause: unknown, reason?: string) => {
8891
const message = cause instanceof Error ? cause.message : "Could not update T3 Connect access.";
8992
const traceId = findErrorTraceId(cause);
9093
console.error("[t3-connect] Could not update T3 Connect", { message, traceId, cause });
9194
setOperationError(traceId ? `${message} Trace ID: ${traceId}` : message);
92-
toastManager.add({
93-
type: "error",
94-
title: "Could not update T3 Connect",
95-
description: message,
96-
data: traceId
97-
? {
98-
secondaryActionProps: {
99-
children: "Copy trace ID",
100-
onClick: () => void navigator.clipboard?.writeText(traceId),
101-
},
102-
}
103-
: undefined,
104-
});
95+
toastManager.add(
96+
stackedThreadToast({
97+
type: "error",
98+
title: "Could not update T3 Connect",
99+
description: reason ?? relayFailureReason(cause) ?? message,
100+
...(traceId
101+
? {
102+
actionProps: {
103+
children: "Copy trace ID",
104+
onClick: () => void navigator.clipboard?.writeText(traceId),
105+
},
106+
actionVariant: "outline",
107+
}
108+
: {}),
109+
data: { hideCopyButton: true },
110+
}),
111+
);
105112
};
106113

107114
const linkData = cloudLinkState.data;
@@ -157,9 +164,19 @@ export function useCloudLinkController(environmentId: EnvironmentId | null) {
157164
refreshManagedTunnelUsage();
158165
if (unlinkResult._tag === "Failure") {
159166
if (!isAtomCommandInterrupted(unlinkResult)) {
160-
reportUpdateFailure(squashAtomCommandFailure(unlinkResult));
167+
const cause = squashAtomCommandFailure(unlinkResult);
168+
// Only the relay delete fails with a relay error, and it runs after the local unlink.
169+
const relayKeptLink =
170+
isCloudEnvironmentLinkError(cause) && isManagedRelayClientError(cause.cause);
171+
reportUpdateFailure(
172+
cause,
173+
relayKeptLink
174+
? "Disconnected here, but T3 Connect still lists it. Remove it in Account."
175+
: undefined,
176+
);
161177
}
162178
cloudLinkState.refresh();
179+
await refreshRelayEnvironments();
163180
return false;
164181
}
165182
} else {

‎apps/web/src/components/clerk/T3ConnectUserProfilePage.tsx‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import {
1313
useManagedRelayEnvironments,
1414
useManagedTunnelUsage,
1515
} from "../../cloud/managedRelayState";
16+
import { relayFailureReason } from "../../cloud/relayFailureReason";
1617
import { relayEnvironmentDiscovery } from "../../state/relay";
1718
import { useAtomCommand } from "../../state/use-atom-command";
1819
import { T3CHAT_SUBSCRIPTION_URL, TunnelUsageMeter, tunnelUpgradeCopy } from "../cloud/TunnelUsage";
1920
import { Button, InlineButton } from "../ui/button";
2021
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
2122
import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty";
2223
import { toastManager } from "../ui/toast";
24+
import { stackedThreadToast } from "../ui/toastHelpers";
2325
import {
2426
ClerkUserProfilePage,
2527
ClerkUserProfileRefreshButton,
@@ -192,9 +194,9 @@ export function T3ConnectUserProfilePage() {
192194
});
193195
mutationPendingRef.current = false;
194196
setDeregisteringEnvironmentId(null);
197+
setConfirmingEnvironmentId(null);
195198

196199
if (result._tag === "Success") {
197-
setConfirmingEnvironmentId(null);
198200
setRemovedEnvironments((current) => {
199201
const linkedAtById = new Map(current.accountId === accountId ? current.linkedAtById : []);
200202
linkedAtById.set(environment.environmentId, environment.linkedAt);
@@ -211,6 +213,8 @@ export function T3ConnectUserProfilePage() {
211213
}
212214
if (isAtomCommandInterrupted(result)) return;
213215

216+
// The relay may have removed it before failing.
217+
environmentsState.refresh();
214218
const cause = squashAtomCommandFailure(result);
215219
const message = cause instanceof Error ? cause.message : "Could not deregister the server.";
216220
const traceId = findErrorTraceId(cause);
@@ -220,19 +224,23 @@ export function T3ConnectUserProfilePage() {
220224
traceId,
221225
cause,
222226
});
223-
toastManager.add({
224-
type: "error",
225-
title: "Could not deregister server",
226-
description: message,
227-
data: traceId
228-
? {
229-
secondaryActionProps: {
230-
children: "Copy trace ID",
231-
onClick: () => void navigator.clipboard?.writeText(traceId),
232-
},
233-
}
234-
: undefined,
235-
});
227+
toastManager.add(
228+
stackedThreadToast({
229+
type: "error",
230+
title: "Could not deregister server",
231+
description: relayFailureReason(cause) ?? message,
232+
...(traceId
233+
? {
234+
actionProps: {
235+
children: "Copy trace ID",
236+
onClick: () => void navigator.clipboard?.writeText(traceId),
237+
},
238+
actionVariant: "outline",
239+
}
240+
: {}),
241+
data: { hideCopyButton: true },
242+
}),
243+
);
236244
};
237245

238246
const removedEnvironmentLinkedAt =

‎apps/web/src/components/clerk/clerkAppearance.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const clerkAppearance = {
2929
colorRing: "var(--ring)",
3030
},
3131
elements: {
32-
modalBackdrop: "dialog-backdrop",
32+
modalBackdrop: "dialog-backdrop z-50",
3333
cardBox: "dialog-glass rounded-2xl border",
3434
card: "border-0 bg-transparent shadow-none",
3535
footer: "bg-transparent bg-none",

‎packages/client-runtime/src/relay/managedRelay.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import * as HttpClientError from "effect/http/HttpClientError";
4747
import type * as HttpMethod from "effect/http/HttpMethod";
4848
import * as HttpApiClient from "effect/http-api/HttpApiClient";
4949
import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
50+
import { relayRequestFailureDetail } from "./errorPresentation.ts";
5051

5152
export interface ManagedRelayDpopProofInput {
5253
readonly method: HttpMethod.HttpMethod;
@@ -159,8 +160,9 @@ export class ManagedRelayRequestFailedError extends Schema.TaggedError<ManagedRe
159160
},
160161
) {
161162
override get message(): string {
162-
const message = `Could not ${this.action}.`;
163-
return this.transportFailed ? `${message} ${NETWORK_BLOCKING_HINT}` : message;
163+
if (this.transportFailed) return `Could not ${this.action}. ${NETWORK_BLOCKING_HINT}`;
164+
const detail = relayRequestFailureDetail(this);
165+
return detail ? `Could not ${this.action}: ${detail}` : `Could not ${this.action}.`;
164166
}
165167
}
166168

0 commit comments

Comments
 (0)