summaryrefslogtreecommitdiff
path: root/op_crates/websocket/01_websocket.js
diff options
context:
space:
mode:
authorLuca Casonato <lucacasonato@yahoo.com>2021-04-02 00:55:22 +0200
committerGitHub <noreply@github.com>2021-04-02 00:55:22 +0200
commit0e72129da2fb3ef5b9ca27552e30264a8fc4c0a7 (patch)
tree2b1e6e1def8c4bbf0a33886bfe0d7b4fd7f6c7a3 /op_crates/websocket/01_websocket.js
parent2d7fdb0a1969128fc0d9a21a8dfaebada325f72d (diff)
fix(websocket): ignore resource close error (#9755)
It is possible that the WebSocket is already closed when we try to close it with `WebSocket#close` or in the `error` or `close` events. Currently this leads to an uncatchable promise rejection. This changes this so that closing an already closed WebSocket is a noop.
Diffstat (limited to 'op_crates/websocket/01_websocket.js')
-rw-r--r--op_crates/websocket/01_websocket.js21
1 files changed, 17 insertions, 4 deletions
diff --git a/op_crates/websocket/01_websocket.js b/op_crates/websocket/01_websocket.js
index 4a303679b..67fc0e481 100644
--- a/op_crates/websocket/01_websocket.js
+++ b/op_crates/websocket/01_websocket.js
@@ -25,6 +25,19 @@
}
}
+ /**
+ * Tries to close the resource (and ignores BadResource errors).
+ * @param {number} rid
+ */
+ function tryClose(rid) {
+ try {
+ core.close(rid);
+ } catch (err) {
+ // Ignore error if the socket has already been closed.
+ if (!(err instanceof Deno.errors.BadResource)) throw err;
+ }
+ }
+
const handlerSymbol = Symbol("eventHandlers");
function makeWrappedHandler(handler) {
function wrappedHandler(...args) {
@@ -125,7 +138,7 @@
const event = new CloseEvent("close");
event.target = this;
this.dispatchEvent(event);
- core.close(this.#rid);
+ tryClose(this.#rid);
});
} else {
this.#readyState = OPEN;
@@ -289,7 +302,7 @@
});
event.target = this;
this.dispatchEvent(event);
- core.close(this.#rid);
+ tryClose(this.#rid);
});
}
}
@@ -350,7 +363,7 @@
});
event.target = this;
this.dispatchEvent(event);
- core.close(this.#rid);
+ tryClose(this.#rid);
break;
}
@@ -365,7 +378,7 @@
const closeEv = new CloseEvent("close");
closeEv.target = this;
this.dispatchEvent(closeEv);
- core.close(this.#rid);
+ tryClose(this.#rid);
break;
}