Skip to content

fix: invalid token wrong recognition #10798

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/rest/src/lib/handlers/Shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,18 @@ export async function handleErrors(
} else {
// Handle possible malformed requests
if (status >= 400 && status < 500) {
// If we receive this status code, it means the token we had is no longer valid.
if (status === 401 && requestData.auth === true) {
// The request will not succeed for some reason, parse the error returned from the api
const data = (await parseResponse(res)) as DiscordErrorData | OAuthErrorData;
const isDiscordError = 'code' in data;
const isOauthError = 'error' in data;

// If we receive the 401 status with 0 code, it means the token we had is no longer valid.
if (status === 401 && requestData.auth === true && (isOauthError || isDiscordError && data.code === 0)) {
manager.setToken(null!);
}

// The request will not succeed for some reason, parse the error returned from the api
const data = (await parseResponse(res)) as DiscordErrorData | OAuthErrorData;
// throw the API error
throw new DiscordAPIError(data, 'code' in data ? data.code : data.error, status, method, url, requestData);
throw new DiscordAPIError(data, isDiscordError ? data.code : data.error, status, method, url, requestData);
}

return res;
Expand Down
Loading