From d30630ba10562bf987f4d2b42440fc41a828119c Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 19 Jun 2024 10:46:29 +0200 Subject: [PATCH] fix: add a noop handler for the error event Backported from main: https://github.com/socketio/socket.io/commit/15af22fc22bc6030fcead322c106f07640336115 --- lib/socket.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index bfdd77e856..cb7ba4123e 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -49,6 +49,8 @@ var flags = [ var emit = Emitter.prototype.emit; +function noop() {} + /** * Interface to a `Client` for a given `Namespace`. * @@ -72,6 +74,9 @@ function Socket(nsp, client, query){ this.fns = []; this.flags = {}; this._rooms = []; + + // prevents crash when the socket receives an "error" event without listener + this.on('error', noop); } /** @@ -427,12 +432,7 @@ Socket.prototype.ondisconnect = function(){ */ Socket.prototype.onerror = function(err){ - if (this.listeners('error').length) { - this.emit('error', err); - } else { - console.error('Missing error handler on `socket`.'); - console.error(err.stack); - } + this.emit('error', err); }; /**