Skip to content

fix(websocket): preserve handshake failure message#5366

Closed
marko1olo wants to merge 1 commit into
nodejs:mainfrom
marko1olo:fix-websocket-error-message
Closed

fix(websocket): preserve handshake failure message#5366
marko1olo wants to merge 1 commit into
nodejs:mainfrom
marko1olo:fix-websocket-error-message

Conversation

@marko1olo
Copy link
Copy Markdown

Fixes #4735.
Refs #4625.

Rationale

Failed WebSocket handshakes already pass a useful failure reason into failWebsocketConnection(), but the later ErrorEvent was built from the close-frame reason. For handshake failures there is no close-frame reason, so event.error.message was empty.

Changes

  • Store the WebSocket failure reason/cause on the shared handler.
  • Build the emitted TypeError from the stored failure reason while leaving CloseEvent.reason tied to the actual close frame.
  • Add a regression test for a local non-101 handshake failure and keep close code 1006 unchanged.

Tests

  • npx borp --timeout 180000 -p "test/websocket/issue-4735.js"
  • npm run lint -- --no-cache lib/web/websocket/connection.js lib/web/websocket/websocket.js test/websocket/issue-4735.js
  • npx borp --timeout 180000 -p "test/websocket/events.js"
  • npx borp --timeout 180000 -p "test/websocket/issue-3506.js"
  • npx borp --timeout 180000 -p "test/websocket/issue-3546.js"
  • git diff --check HEAD~1 HEAD

I also tried broader local websocket coverage, but npm run test:websocket timed out after 5 minutes and test/websocket/opening-handshake.js timed out locally before producing a result.

This change was prepared with OpenAI Codex.

Store the WebSocket failure reason on the handler so the emitted ErrorEvent exposes a non-empty TypeError message while keeping the close event reason tied to the close frame.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: marko1olo <barsukdana@gmail.com>
@KhafraDev KhafraDev closed this Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebSocket emits error event with an empty message when cannot open connection

2 participants