From 2002a2bd6e40f210a7039f904365d04cd0f8561d Mon Sep 17 00:00:00 2001 From: Fleny Date: Fri, 8 Mar 2024 05:42:49 +0100 Subject: [PATCH] feat(rest): Improve rest errors (#3382) * Improve rest errors * formatting --- packages/rest/src/manager.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index 6ad12387e..1dfdd4a76 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -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, }