Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

502 error handling #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shrayus-masanam
Copy link

The Discord API can sometimes return status code 502. According to their docs, you just have to "wait a bit and retry" your request (https://discord.com/developers/docs/topics/opcodes-and-status-codes).

This can be problematic when there isn't proper error handling because file uploads can be interrupted and will never complete. Waiting an arbitrary amount of time (like 10 seconds) and then retrying the request should fix this.
disbox502

Additionally, if one or more requests return status code 429, line 102 (throw new Error(...);) will ALWAYS run after all recursive calls to this.fetchFromApi finish. This means that errors will populate the console even if the upload succeeded with rate limits. I'm not sure if this is intentional, but I changed the conditional to an else if block just in case. Changes are welcomed

}
if (status >= 400) {
} else if (status === 502) {
this.rateLimitWaits[type] = 10 * 1000;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wait seconds should probably be in a const.

@DisboxApp
Copy link
Owner

DisboxApp commented May 18, 2023

Hey!
Thank you so much for the PR :)

This sounds like a good idea, but I have a few questions:

First, about the change from if to else if in line 102 - are you sure that's what you're experiencing? The code is something like this:

if (429)
    return ...
if (>400)
   raise ...

If the status is 429 - the function should return there and never reach the next line. So it shouldn't matter if it's if or else if.

My second question is about the change itself. Is this something you've actually experienced and tested this fixes? It sounds like you're right from the API docs, but we try to reproduce this and see your solution fixes it.

@shrayus-masanam
Copy link
Author

Hey there, my mistake, I misinterpreted the return behavior of the function. if/else if should both work as you said.

As for the 502 change, I haven't been able to reproduce it since it originally happened to me, but I have tested by changing the fetch function:

const realFetch = fetch;
let return502 = true;
fetch = async function() {
    let response = await realFetch(...arguments);
    if (return502 && typeof arguments[0] === "string" && arguments[0].includes("discordapp.com")) {
        console.log("hijacking");
        return502 = false;
        return { // returns a fake 502 response object for the first request
            status: 502,
            headers: response.headers,
            body: null,
            async text() {
                return "whatever it returns"
            }
        }
    }
    return response;
}

This works with the changes that I made, but what I'm noticing now is that in my original screenshot, the error originated from line 84, which is the fetch function call. So I think some additional changes would have to be made to the fetch call to handle the error, maybe something like this:

const response = await fetch(`${this.baseUrl}${path}`, {
    method: method,
    body: body,
}).catch(e => {}); // catching errors

Although what I'm not sure about is why fetch threw an error. Fetch shouldn't reject on HTTP errors: https://developer.mozilla.org/en-US/docs/Web/API/fetch

@SpaceSaver
Copy link

SpaceSaver commented Aug 8, 2023

Although what I'm not sure about is why fetch threw an error. Fetch shouldn't reject on HTTP errors: https://developer.mozilla.org/en-US/docs/Web/API/fetch

Bet it doesn't in FF
Edit: I couldn't get Chrome or Firefox to reject on an HTTP error. Both log an error to console though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants