Skip to content

WebSocket: Received network error or non-101 status code #4273

@rkistner

Description

@rkistner

Bug Description

WebSocket connection errors display a generic Received network error or non-101 status code. error on any connection issue, whether the underlying cause is connection refused, TLS error, protocol upgrade error, or any other issue.

I'm not sure if this is by design, but this makes debugging the issue or displaying an appropriate error to the user difficult.

Reproducible By

import { WebSocket } from 'undici';

// No actual server here
const ws = new WebSocket('ws://localhost:1234');

ws.addEventListener('error', (event) => {
  console.log('error', event.error);
});

Expected Behavior

The error should clearly indicate the cause.

Logs & Screenshots

Error: Received network error or non-101 status code.
    at #onFail (<myproject>/node_modules/undici/lib/web/websocket/websocket.js:469:16)
    at Object.onFail (<myproject>/node_modules/undici/lib/web/websocket/websocket.js:63:43)
    at failWebsocketConnection (<myproject>/node_modules/undici/lib/web/websocket/connection.js:318:11)
    at Object.processResponse (<myproject>/node_modules/undici/lib/web/websocket/connection.js:108:9)
    at <myproject>/node_modules/undici/lib/web/fetch/index.js:1072:19

Environment

Node v24.1.0, undici 7.10.0.

Additional context

My current workaround is to create a custom Dispatcher that records the underlying error. This actually works quite well, and indicates that the underlying cause is available, just not exposed. See the implementation here:
https://gist.github.com/rkistner/a9a000ed584a93c7641272420e8334e7

For reference, I'm using undici instead of the ws package since the Dispatcher support is great - I get much better control over TLS options over a proxy connection.

See more examples of users getting this error here: #3836

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions