错误 [ERR_UNHANDLED_ERROR]:未处理的错误。 (未定义)

问题描述 投票:0回答:1

所以我正在开发一款多人生存类型游戏,在某些时候我有 3 个消息组件收集器相互嵌套,这导致最里面的收集器失败。因此,我设置了它,以便在最外层收集器结束时有一个承诺得到解决,我等待,一旦承诺得到解决,我们就会开始创建下一个收集器和内部收集器,但随后它给出了一个未定义的错误。那永远不会违背诺言。通常我可以修复自己的错误,但这彻底难倒了我。 这是我所说的多人生存游戏的命令。 https://sourceb.in/ebtBGKmQWQ error screenschot

所以我有多个 trycatch 块,第一个是命令文件中的块。第二个是在交互创建事件中。

try {
    await command.execute(interaction, client);
} catch (error) {
    console.log(
        `[INTERACTION CREATE] ${error.stack}\n[INTERACTION CREATE] ${
            interaction.user.username
        } used ${interaction.commandName} in ${interaction.channel.name} at ${new Date(
            Date.now()
        )}`.red
    );
    return await interaction
        .reply({
            content: "There was an error while executing this command!",
            ephemeral: true,
        })
        .catch((e) => {});
}

甚至在上面还有另一个 trycatch 块

try {
    //other things
    //the previous trycatch block
} catch (error) {
    console.log(`[INTERACTION CREATE] ${error.stack}\n${new Date(Date.now())}`.red);
}

然后在我的index.js中我有这些:

process.on("unhandledRejection", (reason, promise) => {
    console.log(
        `[BOT] Unhandled Rejection at ${promise}\n[BOT] Unhandled Rejection reason: ${
            reason.stack
        }\n${new Date(Date.now())}`.red
    );
});

process.on("uncaughtException", (error, origin) => {
    console.log(
        `[BOT] Uncaught Exception at ${origin}\n[BOT] Uncaught Exception error: ${
            error.stack
        }\n${new Date(Date.now())}`.red
    );
});

process.on("uncaughtExceptionMonitor", (error, origin) => {
    console.log(
        `[BOT] Uncaught Exception Monitor at ${origin}\n[BOT] Uncaught Exception Monitor error: ${
            error.stack
        }\n${new Date(Date.now())}`.red
    );
});

我还有一个错误事件,控制台记录了 error.stack。但一切都一直在说 TypeError: Cannot read properties of undefined (reading 'stack') 所以我把所有这些都放在注释中,产生的错误是包含的图像。 正如我所说,通常我可以解决自己的烂摊子,但我什至不知道从哪里开始。

javascript node.js error-handling discord discord.js
1个回答
0
投票

根据您发送的屏幕截图,代码中的某个位置正在抛出

undefined
,并且您的
uncaughtException
处理程序正在尝试读取
(undefined).stack
,导致
Cannot read properties from undefined
错误的无限循环。

注释掉

error.stack
属性访问后,您最终仍然会捕获
undefined
而不是实际的
Error
对象。我有一种感觉,这是由于您拥有所有嵌套的
try
-
catch
块,并且在开发 Discord 机器人时肯定有更好的方法来捕获错误。

在发出

uncaughtException
事件后保持程序运行并不是一个好习惯,因为这意味着你的代码显然有问题,你应该让程序退出。在我的 Discord 机器人项目中,我像这样处理
uncaughtException

process.on('uncaughtException', async err => {
    // Print the exception
    console.error(err);

    // Send yourself the exception (optional)
    await (await client.application.fetch()).owner.send(`\`\`\`js${err.stack}\`\`\``);

    // Disconnect the bot from Discord
    await client.destroy();

    // Perform other resource cleanup as necessary
    // Disconnect from DBs, close open files, etc.

    // Terminate the process with a nonzero exit code
    process.exit(1);
});

使用这种方法将保证 Node 遇到的第一个未捕获的异常将引起您的注意,而不是其他任何事情。

尝试注释掉所有

try
-
catch
逻辑,并使用上面的
uncaughtException
侦听器。请在此处的评论中告诉我情况如何。

© www.soinside.com 2019 - 2024. All rights reserved.