Skip to content

Commit

Permalink
refactor: add a noop handler for the error event
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed May 24, 2023
1 parent d365894 commit 15af22f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ export class Socket<
}
}
this.handshake = this.buildHandshake(auth);

// prevents crash when the socket receives an "error" event without listener
this.on("error", noop);
}

/**
Expand Down Expand Up @@ -720,12 +723,11 @@ export class Socket<
* @private
*/
_onerror(err: Error): void {
if (this.listeners("error").length) {
this.emitReserved("error", err);
} else {
console.error("Missing error handler on `socket`.");
console.error(err.stack);
}
// FIXME the meaning of the "error" event is overloaded:
// - it can be sent by the client (`socket.emit("error")`)
// - 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()`)
this.emitReserved("error", err);
}

/**
Expand Down

0 comments on commit 15af22f

Please sign in to comment.