From 4e5c7869d9d61bf9bf7764a2ad7618d042a5f08d Mon Sep 17 00:00:00 2001 From: meister03 <69507874+meister03@users.noreply.github.com> Date: Fri, 20 May 2022 01:54:05 +0200 Subject: [PATCH] Add Support for Custom Error Converter (#2231) Co-authored-by: meister03 --- rest/convertRestError.ts | 7 +++++++ rest/mod.ts | 1 + rest/processGlobalQueue.ts | 2 -- rest/rest.ts | 2 ++ rest/restManager.ts | 3 +++ rest/runMethod.ts | 4 ++-- 6 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 rest/convertRestError.ts diff --git a/rest/convertRestError.ts b/rest/convertRestError.ts new file mode 100644 index 000000000..4379a4691 --- /dev/null +++ b/rest/convertRestError.ts @@ -0,0 +1,7 @@ + +import { RestRequestRejection } from "./rest.ts"; + +export function convertRestError(errorStack: Error, data: RestRequestRejection): Error { + errorStack.message = `[${data.status}] ${data.error}`; + return errorStack; +} \ No newline at end of file diff --git a/rest/mod.ts b/rest/mod.ts index 0a44a50bd..f881eacf4 100644 --- a/rest/mod.ts +++ b/rest/mod.ts @@ -10,3 +10,4 @@ export * from "./rest.ts"; export * from "./restManager.ts"; export * from "./runMethod.ts"; export * from "./simplifyUrl.ts"; +export * from "./convertRestError.ts"; diff --git a/rest/processGlobalQueue.ts b/rest/processGlobalQueue.ts index b0af07614..d8b18ee7c 100644 --- a/rest/processGlobalQueue.ts +++ b/rest/processGlobalQueue.ts @@ -121,9 +121,7 @@ export async function processGlobalQueue(rest: RestManager) { let json = undefined; if (response.type) { json = JSON.stringify(await response.json()); - console.log(json); } - request.request.reject({ ok: false, status: response.status, diff --git a/rest/rest.ts b/rest/rest.ts index 184de4c89..e3c084bc3 100644 --- a/rest/rest.ts +++ b/rest/rest.ts @@ -9,6 +9,7 @@ import { processRequestHeaders } from "./processRequestHeaders.ts"; import { runMethod } from "./runMethod.ts"; import { runProxyMethod } from "./runProxyMethod.ts"; import { simplifyUrl } from "./simplifyUrl.ts"; +import { convertRestError } from "./convertRestError.ts" export const rest = { /** The bot token for this rest client. */ @@ -52,6 +53,7 @@ export const rest = { runMethod, runProxyMethod, simplifyUrl, + convertRestError, }; export interface RestRequest { diff --git a/rest/restManager.ts b/rest/restManager.ts index 10c1d6ca9..3a2519d64 100644 --- a/rest/restManager.ts +++ b/rest/restManager.ts @@ -6,6 +6,7 @@ import { processQueue } from "./processQueue.ts"; import { processRateLimitedPaths } from "./processRateLimitedPaths.ts"; import { processRequest } from "./processRequest.ts"; import { processRequestHeaders } from "./processRequestHeaders.ts"; +import { convertRestError } from "./convertRestError.ts"; import { RestPayload, RestRateLimitedPath, RestRequest } from "./rest.ts"; import { runMethod } from "./runMethod.ts"; import { simplifyUrl } from "./simplifyUrl.ts"; @@ -71,6 +72,7 @@ export function createRestManager(options: CreateRestManagerOptions) { runMethod: options.runMethod || runMethod, simplifyUrl: options.simplifyUrl || simplifyUrl, processGlobalQueue: options.processGlobalQueue || processGlobalQueue, + convertRestError: options.convertRestError || convertRestError, }; } @@ -91,4 +93,5 @@ export interface CreateRestManagerOptions { runMethod?: typeof runMethod; simplifyUrl?: typeof simplifyUrl; processGlobalQueue?: typeof processGlobalQueue; + convertRestError?: typeof convertRestError; } diff --git a/rest/runMethod.ts b/rest/runMethod.ts index b072bfb45..f5ca39e5c 100644 --- a/rest/runMethod.ts +++ b/rest/runMethod.ts @@ -65,8 +65,8 @@ export async function runMethod( url, method, reject: (data: RestRequestRejection) => { - errorStack.message = `[${data.status}] ${data.error}`; - reject(errorStack); + const restError = rest.convertRestError(errorStack, data); + reject(restError); }, respond: (data: RestRequestResponse) => resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)),