Skip to content

Commit 15af22f

Browse files
refactor: add a noop handler for the error event
We should reduce the scope of the "event" error in the next major version, as it is overloaded today: - it can be sent by the client (`socket.emit("error")`, which is a perfectly valid event name) - it can be emitted when the connection encounters an error (an invalid packet for example) - it can be emitted when a packet is rejected in a middleware (`socket.use()`) Related: #2047
1 parent d365894 commit 15af22f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/socket.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ export class Socket<
280280
}
281281
}
282282
this.handshake = this.buildHandshake(auth);
283+
284+
// prevents crash when the socket receives an "error" event without listener
285+
this.on("error", noop);
283286
}
284287

285288
/**
@@ -720,12 +723,11 @@ export class Socket<
720723
* @private
721724
*/
722725
_onerror(err: Error): void {
723-
if (this.listeners("error").length) {
724-
this.emitReserved("error", err);
725-
} else {
726-
console.error("Missing error handler on `socket`.");
727-
console.error(err.stack);
728-
}
726+
// FIXME the meaning of the "error" event is overloaded:
727+
// - it can be sent by the client (`socket.emit("error")`)
728+
// - it can be emitted when the connection encounters an error (an invalid packet for example)
729+
// - it can be emitted when a packet is rejected in a middleware (`socket.use()`)
730+
this.emitReserved("error", err);
729731
}
730732

731733
/**

0 commit comments

Comments
 (0)