From cd396cf6bdbed20cd6cca4727efe44c1289fc413 Mon Sep 17 00:00:00 2001 From: Fleny Date: Mon, 6 Jan 2025 10:00:46 +0100 Subject: [PATCH] Simplify proxy error code (#4086) --- packages/rest/src/manager.ts | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index f1f79a495..5494b7edb 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -64,7 +64,7 @@ import { getBotIdFromToken, logger, processReactionString, - urlToBase64 + urlToBase64, } from '@discordeno/utils' import { createInvalidRequestBucket } from './invalidBucket.js' import { Queue } from './queue.js' @@ -455,6 +455,9 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage return } + // Consume the response body to avoid leaking memory + await response.arrayBuffer() + rest.logger.debug(`Request to ${url} was ratelimited.`) // Too many attempts, get rid of request from queue. if (options.retryCount >= rest.maxRetryCount) { @@ -473,8 +476,6 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage const resetAfter = response.headers.get(RATE_LIMIT_RESET_AFTER_HEADER) if (resetAfter) await delay(Number(resetAfter) * 1000) - // process the response to prevent mem leak - await response.arrayBuffer() return await options.retryRequest?.(options) } @@ -556,21 +557,13 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage if (!result.ok) { const errText = await result.text().catch(() => null) - if (errText) { - error.cause = { - ok: false, - status: result.status, - body: errText, - } - - throw error + error.cause = { + ok: false, + status: result.status, + body: errText, } - const err = (await result.json().catch(() => {})) as Record - // Legacy Handling to not break old code or when body is missing - if (!err?.body) throw new Error(`Error: ${err.message ?? result.statusText}`) - - throw new Error(JSON.stringify(err)) + throw error } return result.status !== 204 ? await result.json() : undefined