From bc3abbec3259ac28139955a85bc8ad9b018c8d7b Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 21 Nov 2021 13:48:07 +0100 Subject: [PATCH] lib/client: catch handleMessage errors --- lib/client.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 5b81ff1..394caec 100644 --- a/lib/client.js +++ b/lib/client.js @@ -115,7 +115,15 @@ export default class Client extends EventTarget { return; } this.ws.addEventListener("open", this.handleOpen.bind(this)); - this.ws.addEventListener("message", this.handleMessage.bind(this)); + + this.ws.addEventListener("message", (event) => { + try { + this.handleMessage(event); + } catch (err) { + this.dispatchEvent(new CustomEvent("error", { detail: err })); + this.disconnect(); + } + }); this.ws.addEventListener("close", (event) => { console.log("Connection closed (code: " + event.code + ")");