discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.3k stars 3.96k forks source link

`ws` throws `TypeError: Cannot use 'in' operator to search for 'code' in undefined` when with Deno #10327

Closed NeuroWhAI closed 2 months ago

NeuroWhAI commented 3 months ago

Which package is this bug report for?

ws

Issue description

  1. Use deno, Make a simple Discord bot.
  2. Leave the bot running for a long time. (Maybe 1 day?)
  3. The bot suddenly throws an error and dies.

Error : TypeError: Cannot use 'in' operator to search for 'code' in undefined Location: https://github.com/discordjs/discord.js/blob/main/packages/ws/src/ws/WebSocketShard.ts#L794

If I manually modify the conditional statement locally as follows, the problem no longer occurs. But I'm not sure if it's okay to do this.

onError(error) {
  if (error && "code" in error && ["ECONNRESET", "ECONNREFUSED"].includes(error.code)) {

I'm not sure if this is due to differences between Deno and Node.js, but I suspect it is.

Code sample

No response

Versions

Issue priority

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildMessages, MessageContent

I have tested this issue on a development release

No response

didinele commented 3 months ago

Hi. Do you have a full stack trace? I'd like to see what invoked onError

NeuroWhAI commented 3 months ago

I will reproduce and get the trace log.

NeuroWhAI commented 3 months ago

Here is a trace log. @didinele

error: Uncaught TypeError: Cannot use 'in' operator to search for 'code' in undefined
    at WebSocketShard.onError (file:///C:/Users/neurobox/AppData/Local/deno/npm/registry.npmjs.org/@discordjs/ws/1.1.1/dist/index.js:1065:16)
    at WebSocket.connection.onerror (file:///C:/Users/neurobox/AppData/Local/deno/npm/registry.npmjs.org/@discordjs/ws/1.1.1/dist/index.js:683:12)
    at WebSocket.wrappedHandler (ext:deno_web/02_event.js:1401:12)
    at innerInvokeEventListeners (ext:deno_web/02_event.js:754:7)
    at invokeEventListeners (ext:deno_web/02_event.js:801:5)
    at dispatch (ext:deno_web/02_event.js:658:9)
    at WebSocket.dispatchEvent (ext:deno_web/02_event.js:1043:12)
    at WebSocket.[[[eventLoop]]] (ext:deno_websocket/01_websocket.js:471:16)
    at eventLoopTick (ext:core/01_core.js:168:7)
Jiralite commented 3 months ago

If possible, would you be able to log event here so we can see what the payload is?

https://github.com/discordjs/discord.js/blob/757bed0b1f345a8963bc4eb680bed4462531fb49/packages/ws/src/ws/WebSocketShard.ts#L259

NeuroWhAI commented 3 months ago

@Jiralite It was {}... 🤔

    connection.onerror = (event) => {
      console.log("=======================================");
      console.log(JSON.stringify(event));
      console.log("=======================================");
      this.onError(event.error);
    };
=======================================
{}
=======================================
didinele commented 3 months ago

That's most certainly a deno bug of some sort then, recommend reporting it.