From 2678c360071263336fa22af6605c52f61078e74b Mon Sep 17 00:00:00 2001 From: josejooj Date: Mon, 10 Mar 2025 23:07:40 +0000 Subject: [PATCH 1/2] fix invalid token wrong recognition --- packages/rest/src/lib/handlers/Shared.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/rest/src/lib/handlers/Shared.ts b/packages/rest/src/lib/handlers/Shared.ts index 7944f73db24c..dd507c5fce76 100644 --- a/packages/rest/src/lib/handlers/Shared.ts +++ b/packages/rest/src/lib/handlers/Shared.ts @@ -137,15 +137,17 @@ 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; + + // 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 && 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; From d46620480105d9a31685c174a2add86b871bd627 Mon Sep 17 00:00:00 2001 From: josejooj Date: Mon, 10 Mar 2025 23:28:14 +0000 Subject: [PATCH 2/2] fix: handle oauth errors too --- packages/rest/src/lib/handlers/Shared.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/rest/src/lib/handlers/Shared.ts b/packages/rest/src/lib/handlers/Shared.ts index dd507c5fce76..640e4a5399a9 100644 --- a/packages/rest/src/lib/handlers/Shared.ts +++ b/packages/rest/src/lib/handlers/Shared.ts @@ -140,9 +140,10 @@ export async function handleErrors( // 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 && isDiscordError && data.code === 0) { + if (status === 401 && requestData.auth === true && (isOauthError || isDiscordError && data.code === 0)) { manager.setToken(null!); }