Discord.js: Support more complete async stack traces when failures occur during API calls

Created on 4 Sep 2020  Β·  3Comments  Β·  Source: discordjs/discord.js

Is your feature request related to a problem? Please describe.
When errors occur on API calls, it can sometimes be difficult to determine where in my code the call was made.

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (C:\Users\kevin\Desktop\discord.js-async-example\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  method: 'post',
  path: '/channels/471091337459007490/messages',
  code: 50013,
  httpStatus: 403
}

Take the following example, where I purposely denied the "Send Messages" permission in a channel, but send the "test" command anyway:

const Discord = require('discord.js');
const client = new Discord.Client();

const MY_TOKEN = '{INSERT TOKEN}';

async function sendThisMessagePlease(channel, text) {
    await channel.send(text);
}

(async () => {
    client.on('message', async msg => {
        if (msg.content.toLowerCase() !== 'test') {
            return;
        }

        try {
            await sendThisMessagePlease(msg.channel, 'Hello world');
        } catch (error) {
            console.error(error.stack);
        }
    });

    await client.login(MY_TOKEN);
})();

With the "Send Messages" permission missing, the following stack trace is captured:

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (C:\Users\kevin\Desktop\discord.js-async-example\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Notice that this stack trace does not contain any reference to the above source code.

This lack of information seems to be caused by returning a new Promise in RESTManager.js push() and according to this article, the stack trace can be basically lost if you do not use the "async/await" keywords.

Describe the ideal solution
I've made some changes to the RESTManager and RequestHandler in my examples/async-stack-trace branch. This is NOT production code, but just a way to show what the stack traces could look like if async/await was used throughout.

Using the same code example as above, denying the "Send Messages" permission, but using my branch for discord.js, I get a new stack trace with more information:

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (C:\Users\kevin\Desktop\discord.js-async-example\node_modules\discord.js\src\rest\RequestHandler.js:171:15)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async sendThisMessagePlease (C:\Users\kevin\Desktop\discord.js-async-example\example-1.js:7:5)
    at async Client.<anonymous> (C:\Users\kevin\Desktop\discord.js-async-example\example-1.js:17:13)

Notice that this stack trace is more complete, and I can tell exactly where in my code this error is happening.

roadmap REST enhancement

Most helpful comment

This is actually planned and will definitely be available within v13! πŸ˜„ Interesting finds tho πŸ‘

All 3 comments

This is actually planned and will definitely be available within v13! πŸ˜„ Interesting finds tho πŸ‘

Thank you! This would be so helpful for investigating errors. Let me know if you'd like help testing

who are you

Kevin Novak notifications@github.com 于 2020εΉ΄9月4ζ—₯周五 δΈ‹εˆ6:27ε†™ι“οΌš

Thank you! This would be so helpful for investigating errors. Let me know
if you'd like help testing

β€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/discordjs/discord.js/issues/4792#issuecomment-687432183,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ALZBDG4KCW2QBONUXO6RWEDSEFSWLANCNFSM4QZO3EWQ
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  Β·  3Comments

Dmitry221060 picture Dmitry221060  Β·  3Comments

Acaretia picture Acaretia  Β·  3Comments

xCuzImPro picture xCuzImPro  Β·  3Comments

kvn1351 picture kvn1351  Β·  3Comments