mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 17:00:08 +00:00
Add Support for Custom Error Converter (#2231)
Co-authored-by: meister03 <root@_HOSTNAME_>
This commit is contained in:
7
rest/convertRestError.ts
Normal file
7
rest/convertRestError.ts
Normal 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;
|
||||
}
|
||||
@@ -10,3 +10,4 @@ export * from "./rest.ts";
|
||||
export * from "./restManager.ts";
|
||||
export * from "./runMethod.ts";
|
||||
export * from "./simplifyUrl.ts";
|
||||
export * from "./convertRestError.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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)),
|
||||
|
||||
Reference in New Issue
Block a user