【问题标题】:Try to cach UnhandledPromiseRejectionWarning discord.js尝试缓存 UnhandledPromiseRejectionWarning discord.js
【发布时间】:2020-06-25 18:54:20
【问题描述】:

我正在尝试捕获 discord.js 错误 当互联网关闭时会弹出此错误,但我想要一些干净的代码而不是这个凌乱的代码......

我怎么能抓住这个? 我真的什么都试过了..

代码:

(node:11052) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND disc
ordapp.com
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11052) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
 error originated either by throwing inside of an async function without a catch
 block, or by rejecting a promise which was not handled with .catch(). To termin
ate the node process on unhandled promise rejection, use the CLI flag `--unhandl
ed-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejecti
ons_mode). (rejection id: 2)
(node:11052) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr
ecated. In the future, promise rejections that are not handled will terminate th
e Node.js process with a non-zero exit code.

我确实在最顶层尝试过:

process.on('uncaughtException', function (err) {
    //console.log('### BIG ONE (%s)', err);
    console.log("555")
    
});

还有这个:

client.on('error', error => {


    if (error.code === 'ENOTFOUND') {
        
        console.log(no internet!!)
        
    }
});

我也试过这个看看它来自哪里,但没有任何东西显示它仍然是一样的

try {  
  var err = new Error("my error");  
  Error.stackTraceLimit = infinity;  
  throw err;  
} catch(e) {  
  console.log("Error stack trace limit: ")  
  console.log(Error.stackTraceLimit);  
}  
Error stack trace limit:
10
(node:11008) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND disc
ordapp.com

这是我现在使用的导致错误的代码。 我只是想捕捉这样的错误:(无连接)

const Discord = require('discord.js')
const client = new Discord.Client({ autoReconnect: true });
const opn = require('opn')
const getJSON = require('get-json')
const request = require('request');
const config = require("./config/config.json");
const pushbullet = require("./config/pushbullet.json");
const addons = require("./config/addons.json");
const Registration = require("./config/Reg.json");



client.on('uncaughtException', function (err) {
    //console.log('### BIG ONE (%s)', err);
    console.log("555")
    
});



client.login(config.Settings[0].bot_secret_token);

【问题讨论】:

  • 这是您在代码中使用的承诺之一的错误
  • 是的,但我不知道是哪一个...
  • 是的,这就是为什么您在编写代码时将 try/catch 或 .catch() 添加到所有 Promise 中的原因
  • 我确实删除了我的完整代码并让 var 和 const 在那里但根本没有代码.. 它仍然给出了那个错误,我知道当没有互联网时会发生这种情况,因为不和谐不能连接,我只想处理它并让它返回一些自定义消息而不是整个错误
  • @AbhishekRanjan 看到我的代码,我贴出来了。

标签: node.js discord.js


【解决方案1】:

我会尝试用 try/catch 来包装它。 也许添加以下代码以更好地了解正在发生的事情。

Error.stackTraceLimit = Infinity

参考: https://developer.mozilla.org/en-US/docs/Archive/Web/JavaScript/Microsoft_Extensions/Error.stackTraceLimit

问题解决后记得删除,这个不适合生产使用。

【讨论】:

  • 感谢您的回复,我确实这样做了,但没有什么不同。查看编辑的代码
【解决方案2】:

我解决了!!

我把它放在上面,它都解决了。

process.on('unhandledRejection', error => {
    if (error.code === "ENOTFOUND") { console.log ("No internet connection")}
     if (error.code === "ECONNREFUSED") { console.log ("Connection refused")}
    //console.log('Unhandled promise rejection:', error.code);
    
});

【讨论】:

    猜你喜欢
    • 2020-12-01
    • 2021-10-01
    • 2020-04-27
    • 1970-01-01
    • 2014-11-07
    • 2020-08-24
    • 1970-01-01
    • 2021-08-26
    • 2021-07-03
    相关资源
    最近更新 更多