Cannot catch UnhandledPromiseRejectionWarning in promise

a 夏天 提交于 2020-01-15 09:17:10

问题


so I'm trying to catch UnhandledPromiseRejectionWarning in my promise, but for some reason it's not working. It ignores my code and just outputs the error to console.

Error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Forbidden (Cannot send messages to this user)

code:

e.message.author.openDM().then((message) => {
    message.sendMessage(`test`);
}).catch((error) => {
    e.message.channel.sendMessage(error + "test");
});

This is a discord bot, using discordie. In my mind, the above code should send the word "test" to a messages author via private message, if the bot can't, it will send the error and the word test in the channel they sent their message in. However, the second part (inside the catch) doesn't get executed.
tl;dr, The catch in the above code isn't working and I'm getting the above error in console instead if the bot doesn't have permission to dm the user.


回答1:


You forgot the return statement inside the then function. I suppose message.sendMessage('test') returns a promise

e.message.author.openDM().then((message) => {
    return message.sendMessage(`test`);
}).catch((error) => {
    e.message.channel.sendMessage(error + "test");
});


来源:https://stackoverflow.com/questions/46571730/cannot-catch-unhandledpromiserejectionwarning-in-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!