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,