Unhandled 'error' event: "WebSocket was closed before the connection was established" when client was connected and network goes down
See original GitHub issueWhich package is this bug report for?
discord.js
Issue description
Steps to reproduce:
- Create a very simple bot like the code sample below
- Let the bot connect to discord
- Destroy the network/internet connection of the machine running the script (e.g. unplug the (virtual) network cable)
- Wait until the bot tries to reconnect
- See the app crashes on an unhandled ‘error’ event
Error: WebSocket was closed before the connection was established
at WebSocket.close (/home/peter/tmp/node_modules/ws/lib/websocket.js:285:14)
at WebSocketShard.destroy (/home/peter/tmp/node_modules/discord.js/src/client/websocket/WebSocketShard.js:728:27)
at Timeout.<anonymous> (/home/peter/tmp/node_modules/discord.js/src/client/websocket/WebSocketShard.js:522:12)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
Emitted 'error' event on WebSocket instance at:
at emitErrorAndClose (/home/peter/tmp/node_modules/ws/lib/websocket.js:984:13)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Full log: log.txt
There seams to be no way to handle the WebSocket error event.
Expected behavior would be, that discord.js keeps trying to reestablish the connection, or that there is some chance to handle the error.
Came up with this, as my internet connection was gone away and my application using discord.js crashed with the above error.
Code sample
const { Client, Intents } = require('discord.js');
const token = 'xxx';
async function main () {
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
],
});
client.on('debug', console.debug);
client.on('warn', console.warn);
client.on('error', console.error);
client.on('ready', () => console.log('ready'));
await client.login(token);
}
main();
Package version
13.7.0
Node.js version
16.14.2
Operating system
Debian 10
Priority this issue should have
Medium (should be fixed soon)
Which partials do you have configured?
No Partials
Which gateway intents are you subscribing to?
Guilds
I have tested this issue on a development release
No response
Issue Analytics
- State:
- Created a year ago
- Comments:26 (14 by maintainers)
Top Results From Across the Web
node.js - Error: WebSocket was closed before the connection ...
It happens when you call close() on websocket when connection is not established. The code you include in the question does not include...
Read more >error: websocket was closed before the connection ... - You.com
It happens when you call close() on websocket when connection is not established. The code you include in the question does not include...
Read more >Error with WebSocket - Microsoft Q&A
Hello I get the error: WebSocket connection to 'wss://localhost:54970/Dashboard.Server/' failed: Error in connection establishment: net:: ...
Read more >WebSocket: error event - Web APIs | MDN
The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent...
Read more >Why Am I Receiving "Websocket Is Closed Before ... - Pusher
The WebSocket is closed before the connection is established error message indicates that some client code, or other mechanism, has closed the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@legendhimslef Using your PR #7581 seams to work fine. The error then gets emitted as
shardError
and a reconnect is queued. 👍@pedroricardo Additionally to the thing above, please also have an error handler like I showed.