fix(rest): errors returned from makeRequest() doesn't provide more info rest is proxied (#4012)

This commit is contained in:
Awesome Stickz
2024-11-23 14:54:18 -06:00
committed by GitHub
parent 91d75823eb
commit e8391e8724
+17 -4
View File
@@ -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<string, any>
// 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,