Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TypedMessenger): Make sure errors while handling received messages are caught correctly #957

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/util/TypedMessenger/TypedMessenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ export class TypedMessenger {
try {
if (typeof message.data == "string") {
const parsed = JSON.parse(message.data);
this.handleReceivedMessage(parsed);
await this.handleReceivedMessage(parsed);
}
} catch (e) {
console.error("An error occurred while handling a websocket message.", message.data, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TypedMessenger } from "../../../../../../src/util/TypedMessenger/TypedM
import { assertSpyCalls, stub } from "std/testing/mock.ts";
import { assertPromiseResolved } from "../../../../../../src/util/asserts.js";
import { createLinkedMessengers, createLinkedWebSockets } from "./shared/websockets.js";
import { waitForMicrotasks } from "../../../../../../src/util/waitForMicroTasks.js";

Deno.test({
name: "initializeWebSocket()",
Expand Down Expand Up @@ -78,3 +79,28 @@ Deno.test({
}
},
});

Deno.test({
name: "Errors due to websocket closing before a response message is sent are caught",
async fn() {
const consoleSpy = stub(console, "error", () => {});

try {
const { socketA, socketB } = createLinkedWebSockets();
const { messengerB } = createLinkedMessengers(socketA, socketB, {
foo() {
return "foo";
},
}, {});
socketA.close();
const sendPromise = messengerB.send.foo();

await waitForMicrotasks();
assertSpyCalls(consoleSpy, 1);
assertEquals(consoleSpy.calls[0].args[0], "An error occurred while handling a websocket message.");
await assertPromiseResolved(sendPromise, false);
} finally {
consoleSpy.restore();
}
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class FakeWebSocket extends EventTarget {
* @param {string} data
*/
send(data) {
if (this.#readyState != WebSocket.OPEN) {
throw new DOMException("readyState not OPEN", "InvalidStateError");
}
this.#otherSocket?.dispatchEvent(new MessageEvent("message", {
data,
}));
Expand All @@ -24,6 +27,11 @@ class FakeWebSocket extends EventTarget {
this.dispatchEvent(new Event("open"));
}

close() {
this.#readyState = WebSocket.CLOSED;
this.dispatchEvent(new Event("close"));
}

error() {
this.dispatchEvent(new Event("error"));
}
Expand Down
Loading