Skip to content

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

@piotr-cz

Description

@piotr-cz

Bug Description

undici >= 7.16.0 emits ErrorEvent with such error property:

{
  "name": "TypeError",
  "message": ""
}

undici < 7.16.0 emits ErrorEvent with such error property:

{
  "name": "Error",
  "message": "Received network error or non-101 status code."
}

Reproducible By

import { WebSocket } from 'undici'
import { type TestContext, test } from 'node:test'

test('should emit error event with Error message', { plan: 1 }, (t: TestContext, done): void => {
  const webSocket = new WebSocket('https://not-a-ws-server.com/')

  webSocket.onerror = ({ error }) => {
    done(
      t.assert.deepStrictEqual(
        {
          name: error.name,
          message: error.message,
        },
        {
          name: 'Error',
          message: 'Received network error or non-101 status code.',
        }
      )
    )
  }
})

Expected Behavior

Error should have any message explaining why event occurred.

Logs & Screenshots

  AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
  + actual - expected

    {
  +   message: '',
  +   name: 'TypeError'
  -   message: 'Received network error or non-101 status code.',
  -   name: 'Error'
    }

Environment

Windows, Node v25.3.0

Additional context

MDN doesn't describe WebSocket error event listener argument as ErrorEvent, just plain Event which doesn't have error property.

But since undici uses ErrorEvent, it should have some message, not just an empty string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwebsocketPull requests or issues related to websocket and its standard

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions