chore: monorepo setup (#7175)

This commit is contained in:
Noel
2022-01-07 17:18:25 +01:00
committed by GitHub
parent 780b7ed39f
commit 16390efe6e
504 changed files with 25459 additions and 22830 deletions

View File

@@ -0,0 +1,107 @@
import type { InternalRequest, RawAttachment } from '../RequestManager';
interface DiscordErrorFieldInformation {
code: string;
message: string;
}
interface DiscordErrorGroupWrapper {
_errors: DiscordError[];
}
type DiscordError = DiscordErrorGroupWrapper | DiscordErrorFieldInformation | { [k: string]: DiscordError } | string;
export interface DiscordErrorData {
code: number;
message: string;
errors?: DiscordError;
}
export interface OAuthErrorData {
error: string;
error_description?: string;
}
export interface RequestBody {
attachments: RawAttachment[] | undefined;
json: unknown | undefined;
}
function isErrorGroupWrapper(error: DiscordError): error is DiscordErrorGroupWrapper {
return Reflect.has(error as Record<string, unknown>, '_errors');
}
function isErrorResponse(error: DiscordError): error is DiscordErrorFieldInformation {
return typeof Reflect.get(error as Record<string, unknown>, 'message') === 'string';
}
/**
* Represents an API error returned by Discord
* @extends Error
*/
export class DiscordAPIError extends Error {
public requestBody: RequestBody;
/**
* @param rawError The error reported by Discord
* @param code The error code reported by Discord
* @param status The status code of the response
* @param method The method of the request that erred
* @param url The url of the request that erred
* @param bodyData The unparsed data for the request that errored
*/
public constructor(
public rawError: DiscordErrorData | OAuthErrorData,
public code: number | string,
public status: number,
public method: string,
public url: string,
bodyData: Pick<InternalRequest, 'attachments' | 'body'>,
) {
super(DiscordAPIError.getMessage(rawError));
this.requestBody = { attachments: bodyData.attachments, json: bodyData.body };
}
/**
* The name of the error
*/
public override get name(): string {
return `${DiscordAPIError.name}[${this.code}]`;
}
private static getMessage(error: DiscordErrorData | OAuthErrorData) {
let flattened = '';
if ('code' in error) {
if (error.errors) {
flattened = [...this.flattenDiscordError(error.errors)].join('\n');
}
return error.message && flattened
? `${error.message}\n${flattened}`
: error.message || flattened || 'Unknown Error';
}
return error.error_description ?? 'No Description';
}
private static *flattenDiscordError(obj: DiscordError, key = ''): IterableIterator<string> {
if (isErrorResponse(obj)) {
return yield `${key.length ? `${key}[${obj.code}]` : `${obj.code}`}: ${obj.message}`.trim();
}
for (const [k, v] of Object.entries(obj)) {
const nextKey = k.startsWith('_') ? key : key ? (Number.isNaN(Number(k)) ? `${key}.${k}` : `${key}[${k}]`) : k;
if (typeof v === 'string') {
yield v;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
} else if (isErrorGroupWrapper(v)) {
for (const error of v._errors) {
yield* this.flattenDiscordError(error, nextKey);
}
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
yield* this.flattenDiscordError(v, nextKey);
}
}
}
}

View File

@@ -0,0 +1,30 @@
import type { InternalRequest } from '../RequestManager';
import type { RequestBody } from './DiscordAPIError';
/**
* Represents a HTTP error
*/
export class HTTPError extends Error {
public requestBody: RequestBody;
/**
* @param message The error message
* @param name The name of the error
* @param status The status code of the response
* @param method The method of the request that erred
* @param url The url of the request that erred
* @param bodyData The unparsed data for the request that errored
*/
public constructor(
message: string,
public override name: string,
public status: number,
public method: string,
public url: string,
bodyData: Pick<InternalRequest, 'attachments' | 'body'>,
) {
super(message);
this.requestBody = { attachments: bodyData.attachments, json: bodyData.body };
}
}

View File

@@ -0,0 +1,30 @@
import type { RateLimitData } from '../REST';
export class RateLimitError extends Error implements RateLimitData {
public timeToReset: number;
public limit: number;
public method: string;
public hash: string;
public url: string;
public route: string;
public majorParameter: string;
public global: boolean;
public constructor({ timeToReset, limit, method, hash, url, route, majorParameter, global }: RateLimitData) {
super();
this.timeToReset = timeToReset;
this.limit = limit;
this.method = method;
this.hash = hash;
this.url = url;
this.route = route;
this.majorParameter = majorParameter;
this.global = global;
}
/**
* The name of the error
*/
public override get name(): string {
return `${RateLimitError.name}[${this.route}]`;
}
}