本文介绍了Node.js 未处理的“错误"事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的 Discord bot 在线时间过长(大约 3-4 小时)时,随机出现此错误,但有时该错误会早晚出现.这真的让我很困扰.

This error randomly appears When leaving my discord bot online for too long, around 3-4 hours but sometimes the error occurs sooner and sometimes later. It's really bothering me.

  events.js:188
      throw err;
      ^

Error: Unhandled "error" event. ([object Object])
    at Client.emit (events.js:186:19)
    at WebSocketConnection.onError (D:\BasementMonster\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:374:17)
    at WebSocket.onError (D:\BasementMonster\node_modules\ws\lib\event-target.js:128:16)
    at emitOne (events.js:116:13)
    at WebSocket.emit (events.js:211:7)
    at _receiver.cleanup (D:\BasementMonster\node_modules\ws\lib\websocket.js:211:14)
    at Receiver.cleanup (D:\BasementMonster\node_modules\ws\lib\receiver.js:557:13)
    at WebSocket.finalize (D:\BasementMonster\node_modules\ws\lib\websocket.js:206:20)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

推荐答案

我在自己的代码中与这个问题搏斗了一段时间.主要问题是跟踪完全没有帮助, 错误发生的频率如此之低,以至于在终端中运行它并等待"成为一项徒劳的任务.最终,我发现 Discord.js 客户端本身抛出了一个错误——这在我阅读的任何文档中都没有提到,所以我没有处理它——因为这个 oopsie 发生在Discord.js 包本身,我的代码中没有任何行可以在跟踪中指向它.

I wrestled with this problem for a while in my own code. The main problem is that the trace is completely unhelpful, and that the error occurs so infrequently as to make "run it in the terminal and wait" a futile task. Eventually, I was able to figure out that the Discord.js client itself was throwing an error -- this wasn't mentioned in any of the documentation I read, so I had no handler for it -- and since this oopsie happens in the Discord.js package itself, there was no line in my code for it to point to in the trace.

代码中某处需要存在的东西

What needs to exist in the code somewhere is something along the lines of

client.on('error', (err) => {
   console.log(err.message)
});

当然,这会根据您的 Client 对象以及您想要的错误处理进行调整.据我所知,这个特定的跟踪来自失去 Internet 连接,这是我想要记录的事情,所以我调用了自己的自定义函数,而不是 console.log() 将事件写入日志文件带有时间戳.

This is adjusted, of course, for whatever your Client object is, and whatever you want the error handling to be.This specific trace, as far as I can tell, comes from losing Internet connection, which is something I want to have a record of, so instead of console.log() I called my own custom function that writes the event out to a logfile with a timestamp.

通过运行机器人的一个实例并终止我的互联网连接来测试这一点,结果表明不仅日志功能有效,而且 Discord.js 在返回后自动恢复了机器人的会话.(您的里程可能会有所不同.)

Testing this by running an instance of the bot and killing my Internet connection revealed that not only did the logging function work, but that Discord.js automatically restored the bot's session after it came back. (Your mileage may vary.)

这篇关于Node.js 未处理的“错误"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:24