Handle websocket constructor failures in browser adapter

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 00:07:14 +00:00
1 parent 694818e5b3
commit e01496a19c
2 files changed
+22 -1

No files matched your search

+14
View File
@@ -7,6 +7,7 @@ class MockWebSocket {
static instances: MockWebSocket[] = [];
static failSend = false;
static failOpen = false;
static failConstruct = false;
readyState = 0;
binaryType = "blob";
@@ -14,6 +15,9 @@ class MockWebSocket {
private listeners: Record<string, Listener[]> = {};
constructor(readonly url: string) {
if (MockWebSocket.failConstruct) {
throw new Error("mock constructor failure");
}
MockWebSocket.instances.push(this);
queueMicrotask(() => {
if (MockWebSocket.failOpen) {
@@ -97,6 +101,7 @@ describe("wsNativeApi", () => {
MockWebSocket.instances = [];
MockWebSocket.failSend = false;
MockWebSocket.failOpen = false;
MockWebSocket.failConstruct = false;
vi.stubGlobal("WebSocket", MockWebSocket as unknown as typeof WebSocket);
});
@@ -883,6 +888,15 @@ describe("wsNativeApi", () => {
await expect(api.todos.list()).rejects.toThrow("Failed to connect to local t3 runtime.");
});
it("rejects requests when websocket construction throws", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4426");
MockWebSocket.failConstruct = true;
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
const api = getOrCreateWsNativeApi();
await expect(api.todos.list()).rejects.toThrow("Failed to connect to local t3 runtime.");
});
it("accepts arraybuffer server messages", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4406");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
+8 -1
View File
@@ -42,7 +42,14 @@ class WsNativeApiClient {
}
this.connectPromise = new Promise<WebSocket>((resolve, reject) => {
const socket = new WebSocket(this.wsUrl);
let socket: WebSocket;
try {
socket = new WebSocket(this.wsUrl);
} catch {
this.connectPromise = null;
reject(new Error("Failed to connect to local t3 runtime."));
return;
}
socket.binaryType = "arraybuffer";
this.socket = socket;