Skip to content

Support more complete async stack traces when failures occur during API calls #4792

@KevinNovak

Description

@KevinNovak

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions