Add Support for Custom Error Converter (#2231)

Co-authored-by: meister03 <root@_HOSTNAME_>
This commit is contained in:
meister03
2022-05-20 01:54:05 +02:00
committed by GitHub
parent f7e11d6ea6
commit 4e5c7869d9
6 changed files with 15 additions and 4 deletions

7
rest/convertRestError.ts Normal file
View File

@@ -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;
}

View File

@@ -10,3 +10,4 @@ export * from "./rest.ts";
export * from "./restManager.ts";
export * from "./runMethod.ts";
export * from "./simplifyUrl.ts";
export * from "./convertRestError.ts";

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -65,8 +65,8 @@ export async function runMethod<T = any>(
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)),