Repository navigation
Conversation
Since 7.16.0 (nodejs#4521) the ErrorEvent emitted when a WebSocket connection could not be opened carried an empty string message (new TypeError('')), losing the reason the connection failed. Propagate the failure reason and cause from failWebsocketConnection to #onSocketClose through a new handler.closeInfo slot, so the ErrorEvent's error.message and message fields describe the underlying cause (e.g. "Received network error or non-101 status code.") as they did before. Refs nodejs#4735 Signed-off-by: Maruthan G <maruthang4@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5079 +/- ##
==========================================
+ Coverage 93.10% 93.12% +0.02%
==========================================
Files 110 110
Lines 35807 35824 +17
==========================================
+ Hits 33339 33362 +23
+ Misses 2468 2462 -6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
KhafraDev
requested changes
Apr 20, 2026
KhafraDev
left a comment
Member
There was a problem hiding this comment.
This just isn't good code, and we're already not following the spec by emitting an ErrorEvent.
Contributor
Author
|
Understood @KhafraDev — thanks for the direction. If the Sorry for the noise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This relates to...
Fixes #4735
Rationale
Since undici 7.16.0 (PR #4521, commit
909a5845), theErrorEventdispatched on aWebSocketwhen the opening handshake fails carries an empty string as itsmessage. The old behavior surfaced a descriptive reason (e.g."Received network error or non-101 status code."), which is what consumers — and the WPT / nodejs test fixtures — rely on to diagnose connection failures.The regression comes from
WebSocket#onSocketCloseunconditionally constructingnew TypeError(reason)wherereasondefaulted to"", discarding the descriptive reason thatfailWebsocketConnectionhad already produced internally.This change restores the pre-regression behavior by plumbing the failure reason (and
cause, when present) fromfailWebsocketConnectionthrough to#onSocketClose, so the dispatchedErrorEventonce again has a non-emptymessageand itserroris aTypeErrorwith a meaningfulmessage(and acausewhen the underlying failure provides one). The error type remainsTypeError, consistent with maintainer guidance on the duplicate issue #4625.Changes
lib/web/websocket/connection.js:failWebsocketConnectionnow stores{ code, reason, cause }on a newhandler.closeInfoslot before invoking the socket close path, so the reason is not lost by the time#onSocketCloseruns.lib/web/websocket/websocket.js: handler typedef and initialization gain acloseInfoslot;#onSocketCloseconsumes it to build anErrorEventwhosemessagematches the pre-regression string and whoseerroris aTypeErrorcarrying that samemessage(pluscausewhen available).test/websocket/issue-4735.js: new regression test that opens aWebSocketto a closed port and asserts theerrorevent'smessageis a non-empty string.Features
N/A
Bug Fixes
ErrorEventmessage/errorwith the underlying failure reason on WebSocket connect failures (WebSocket emits error event with an empty message when cannot open connection #4735).Breaking Changes and Deprecations
None.
Status