Surface websocket close metadata in renderer connection errors
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
4966661956
commit
d5f970c961
2 files changed
+65
-10
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { WS_CLOSE_CODES, WS_CLOSE_REASONS } from "@acme/contracts";
|
||||
|
||||
type Listener = (event: unknown) => void;
|
||||
|
||||
@@ -23,7 +24,10 @@ class MockWebSocket {
|
||||
queueMicrotask(() => {
|
||||
if (MockWebSocket.failCloseBeforeOpen) {
|
||||
this.readyState = 3;
|
||||
this.emit("close", { code: 4001 });
|
||||
this.emit("close", {
|
||||
code: WS_CLOSE_CODES.unauthorized,
|
||||
reason: WS_CLOSE_REASONS.unauthorized,
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (MockWebSocket.failOpen) {
|
||||
@@ -406,7 +410,7 @@ describe("wsNativeApi", () => {
|
||||
await waitForCondition(() => (socket?.sentMessages.length ?? 0) > 0);
|
||||
socket?.close();
|
||||
|
||||
await expect(request).rejects.toThrow("websocket disconnected");
|
||||
await expect(request).rejects.toThrow("websocket disconnected (code 1000)");
|
||||
});
|
||||
|
||||
it("reconnects on subsequent requests after websocket close", async () => {
|
||||
@@ -1001,7 +1005,9 @@ describe("wsNativeApi", () => {
|
||||
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = getOrCreateWsNativeApi();
|
||||
|
||||
await expect(api.todos.list()).rejects.toThrow("Failed to connect to local t3 runtime.");
|
||||
await expect(api.todos.list()).rejects.toThrow(
|
||||
"Failed to connect to local t3 runtime: unauthorized websocket connection.",
|
||||
);
|
||||
});
|
||||
|
||||
it("recovers after websocket pre-open close on a later request", async () => {
|
||||
@@ -1010,7 +1016,9 @@ describe("wsNativeApi", () => {
|
||||
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
|
||||
const api = getOrCreateWsNativeApi();
|
||||
|
||||
await expect(api.todos.list()).rejects.toThrow("Failed to connect to local t3 runtime.");
|
||||
await expect(api.todos.list()).rejects.toThrow(
|
||||
"Failed to connect to local t3 runtime: unauthorized websocket connection.",
|
||||
);
|
||||
|
||||
MockWebSocket.failCloseBeforeOpen = false;
|
||||
const secondRequest = api.todos.list();
|
||||
|
||||
@@ -9,7 +9,12 @@ import type {
|
||||
OutputChunk,
|
||||
AgentExit,
|
||||
} from "@acme/contracts";
|
||||
import { WS_EVENT_CHANNELS, wsServerMessageSchema } from "@acme/contracts";
|
||||
import {
|
||||
WS_CLOSE_CODES,
|
||||
WS_CLOSE_REASONS,
|
||||
WS_EVENT_CHANNELS,
|
||||
wsServerMessageSchema,
|
||||
} from "@acme/contracts";
|
||||
|
||||
type PendingRequest = {
|
||||
resolve: (value: unknown) => void;
|
||||
@@ -21,6 +26,48 @@ type SubscriptionSet<TValue> = Set<(value: TValue) => void>;
|
||||
const REQUEST_TIMEOUT_MS = 30_000;
|
||||
const textDecoder = new TextDecoder();
|
||||
|
||||
function closeDetailsFromEvent(event: unknown) {
|
||||
const code = (event as { code?: unknown } | null)?.code;
|
||||
const reason = (event as { reason?: unknown } | null)?.reason;
|
||||
return {
|
||||
code: typeof code === "number" ? code : null,
|
||||
reason: typeof reason === "string" ? reason : null,
|
||||
};
|
||||
}
|
||||
|
||||
function runtimeConnectErrorFromClose(event: unknown) {
|
||||
const { code, reason } = closeDetailsFromEvent(event);
|
||||
if (code === WS_CLOSE_CODES.unauthorized || reason === WS_CLOSE_REASONS.unauthorized) {
|
||||
return new Error("Failed to connect to local t3 runtime: unauthorized websocket connection.");
|
||||
}
|
||||
if (
|
||||
code === WS_CLOSE_CODES.replacedByNewClient ||
|
||||
reason === WS_CLOSE_REASONS.replacedByNewClient
|
||||
) {
|
||||
return new Error(
|
||||
"Failed to connect to local t3 runtime: replaced by a newer websocket client.",
|
||||
);
|
||||
}
|
||||
if (code === null) {
|
||||
return new Error("Failed to connect to local t3 runtime.");
|
||||
}
|
||||
if (!reason || reason.length === 0) {
|
||||
return new Error(`Failed to connect to local t3 runtime (close code ${code}).`);
|
||||
}
|
||||
return new Error(`Failed to connect to local t3 runtime (close code ${code}: ${reason}).`);
|
||||
}
|
||||
|
||||
function requestDisconnectError(id: string, event: unknown) {
|
||||
const { code, reason } = closeDetailsFromEvent(event);
|
||||
if (code === null) {
|
||||
return new Error(`Request ${id} failed: websocket disconnected.`);
|
||||
}
|
||||
if (!reason || reason.length === 0) {
|
||||
return new Error(`Request ${id} failed: websocket disconnected (code ${code}).`);
|
||||
}
|
||||
return new Error(`Request ${id} failed: websocket disconnected (code ${code}: ${reason}).`);
|
||||
}
|
||||
|
||||
class WsNativeApiClient {
|
||||
private socket: WebSocket | null = null;
|
||||
private connectPromise: Promise<WebSocket> | null = null;
|
||||
@@ -53,13 +100,13 @@ class WsNativeApiClient {
|
||||
this.socket = socket;
|
||||
let hasOpened = false;
|
||||
let connectionSettled = false;
|
||||
const rejectConnection = () => {
|
||||
const rejectConnection = (error?: Error) => {
|
||||
if (connectionSettled) {
|
||||
return;
|
||||
}
|
||||
connectionSettled = true;
|
||||
this.connectPromise = null;
|
||||
reject(new Error("Failed to connect to local t3 runtime."));
|
||||
reject(error ?? new Error("Failed to connect to local t3 runtime."));
|
||||
};
|
||||
const resolveConnection = () => {
|
||||
if (connectionSettled) {
|
||||
@@ -83,15 +130,15 @@ class WsNativeApiClient {
|
||||
void this.handleMessage(event.data);
|
||||
});
|
||||
|
||||
socket.addEventListener("close", () => {
|
||||
socket.addEventListener("close", (event) => {
|
||||
this.socket = null;
|
||||
if (!hasOpened) {
|
||||
rejectConnection();
|
||||
rejectConnection(runtimeConnectErrorFromClose(event));
|
||||
return;
|
||||
}
|
||||
for (const [id, pending] of this.pending.entries()) {
|
||||
clearTimeout(pending.timeout);
|
||||
pending.reject(new Error(`Request ${id} failed: websocket disconnected.`));
|
||||
pending.reject(requestDisconnectError(id, event));
|
||||
}
|
||||
this.pending.clear();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user