feat(rest): Improve rest errors (#3382)

* Improve rest errors

* formatting
This commit is contained in:
Fleny
2024-03-08 05:42:49 +01:00
committed by GitHub
parent edbb134efa
commit 2002a2bd6e

View File

@@ -1,6 +1,4 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable no-const-assign */
import { Buffer } from 'node:buffer'
import { calculateBits, camelize, camelToSnakeCase, delay, getBotIdFromToken, logger, processReactionString, urlToBase64 } from '@discordeno/utils'
@@ -479,6 +477,10 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage
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.'
// eslint-disable-next-line no-async-promise-executor
return await new Promise(async (resolve, reject) => {
const payload: SendRequestOptions = {
@@ -486,13 +488,16 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage
method,
requestBodyOptions: options,
retryCount: 0,
retryRequest: async function (payload: SendRequestOptions) {
retryRequest: async (payload: SendRequestOptions) => {
await rest.processRequest(payload)
},
resolve: (data) => {
resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined)
},
reject,
reject: (reason) => {
error.cause = reason
reject(error)
},
runThroughQueue: options?.runThroughQueue,
}