From e8391e872476bd3aa0d1caf0e9081b53ba8b7d23 Mon Sep 17 00:00:00 2001 From: Awesome Stickz Date: Sun, 24 Nov 2024 02:24:18 +0530 Subject: [PATCH] fix(rest): errors returned from makeRequest() doesn't provide more info rest is proxied (#4012) --- packages/rest/src/manager.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index 2c63760d9..360fc5183 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -533,6 +533,10 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage }, async makeRequest(method, route, options) { + // This error needs to be created here because of how stack traces get calculated + const error = new Error() + error.message = 'Failed to send request to discord.' + if (rest.isProxied) { if (rest.authorization) { options ??= {} @@ -543,19 +547,28 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage const result = await fetch(`${rest.baseUrl}/v${rest.version}${route}`, rest.createRequestBody(method, options)) if (!result.ok) { + const errText = await result.text().catch(() => null) + + if (errText) { + error.cause = { + ok: false, + status: result.status, + body: errText, + } + + throw error + } + 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)) } return result.status !== 204 ? await result.json() : undefined } - // This error needs to be created here because of how stack traces get calculated - const error = new Error() - error.message = 'Failed to send request to discord.' - return await new Promise(async (resolve, reject) => { const payload: SendRequestOptions = { route,