discordjs / discord.js

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

throw err; // Unhandled 'error' event - Error: connect ETIMEDOUT 1 #3518

Closed RecoX closed 5 years ago

RecoX commented 5 years ago

Please describe the problem you are having in as much detail as possible: I have this error time to time in my node.js app

events.js:189
    throw err; // Unhandled 'error' event
    ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. (ErrorEvent {
  target:
   WebSocket {
     _events:
      [Object: null prototype] {
        message: [Function],
        open: [Function],
        error: [Function],
        close: [Function] },
     _eventsCount: 4,
     _maxListeners: undefined,
     readyState: 2,
     protocol: '',
     _binaryType: 'nodebuffer',
     _closeFrameReceived: false,
     _closeFrameSent: false,
     _closeMessage: '',
     _closeTimer: null,
     _closeCode: 1006,
     _extensions: {},
     _receiver: null,
     _sender: null,
     _socket: null,
     _isServer: false,
     _redirects: 0,
     url: 'wss://gateway.discord.gg/?v=6&encoding=json',
     _req: null },
  type: 'error',
  message: 'connect ETIMEDOUT 162.159.134.234:443',
  error:
   { Error: connect ETIMEDOUT 162.159.134.234:443
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
     errno: 'ETIMEDOUT',
     code: 'ETIMEDOUT',
     syscall: 'connect',
     address: '162.159.134.234',
     port: 443 } })
    at Client.emit (events.js:187:17)
    at WebSocketConnection.onError (C:\Users\Administrator\Desktop\alkon\ao-api-server\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:374:17)
    at WebSocket.onError (C:\Users\Administrator\Desktop\alkon\ao-api-server\node_modules\ws\lib\event-target.js:128:16)
    at WebSocket.emit (events.js:198:13)
    at ClientRequest.req.on (C:\Users\Administrator\Desktop\alkon\ao-api-server\node_modules\ws\lib\websocket.js:568:15)
    at ClientRequest.emit (events.js:198:13)
    at TLSSocket.socketErrorListener (_http_client.js:392:9)
    at TLSSocket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ao-api-server@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ao-api-server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2019-10-02T02_26_20_180Z-debug.log

Include a reproducible code sample here, if possible: Well there are 2 kinds of functions I use:

app.post("/sendHappyHourEndMessage/", function (req, res) {
    let message = req.body.message

    const embed = new Discord.RichEmbed()
        // Set the title of the field
        .setTitle(`Termino el Happy Hour!!`)
        .setImage('https://raw.githubusercontent.com/ao-libre/ao-api-server/master/resources/images/happy-hour-end-discord.png')
        .setFooter(website, iconFooter)
        // Set the color of the embed
        .setColor(0x641b06)
        // Set the main content of the embed
        .setDescription(message);

    const channel = global.clientDiscord.channels.find(x => x.name === "general")
    channel.send(embed)
    return res.status(200).json(embed);
});

And

clientDiscord.on('message', message => {
    if (message.content === 'ping') {
        message.reply('pong');
    }

    if (message.content === '/online') {
        const usersOnline = getOnlineUsersQuantityInServer()
        message.reply(`En este momento hay: ${usersOnline} conectados en el servidor de Rol Alkon 0.13.X`);
    }
});

Further details:

https://github.com/discordjs/discord.js/issues/2523

I think this is related, but as they said in the docs there is no info for error handling

monbrey commented 5 years ago

As per #2523 which you seem to have read, you need to attach a listener to the Client#error event.

This particular error is ETIMEDOUT which is just a connection issue with Discord. Not a bug.

If you need help with discord.js installation or usage, please go to the discord.js Discord server instead: https://discord.gg/bRCvFy9

This issue tracker is only for bug reports and enhancement suggestions.

RecoX commented 5 years ago

Yes we can close this as here https://github.com/discordjs/discord.js/issues/2879 everything is clear

although i recommend put something in the documentation to avoid more issues related with this, so everyone knows how to handle this easily :)