mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23:31 +01:00
refactor: use eslint-config-neon for packages. (#8579)
Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { InternalRequest, RawFile } from '../RequestManager';
|
||||
import type { InternalRequest, RawFile } from '../RequestManager.js';
|
||||
|
||||
interface DiscordErrorFieldInformation {
|
||||
code: string;
|
||||
@@ -9,12 +9,12 @@ interface DiscordErrorGroupWrapper {
|
||||
_errors: DiscordError[];
|
||||
}
|
||||
|
||||
type DiscordError = DiscordErrorGroupWrapper | DiscordErrorFieldInformation | { [k: string]: DiscordError } | string;
|
||||
type DiscordError = DiscordErrorFieldInformation | DiscordErrorGroupWrapper | string | { [k: string]: DiscordError };
|
||||
|
||||
export interface DiscordErrorData {
|
||||
code: number;
|
||||
message: string;
|
||||
errors?: DiscordError;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface OAuthErrorData {
|
||||
@@ -37,7 +37,6 @@ function isErrorResponse(error: DiscordError): error is DiscordErrorFieldInforma
|
||||
|
||||
/**
|
||||
* Represents an API error returned by Discord
|
||||
* @extends Error
|
||||
*/
|
||||
export class DiscordAPIError extends Error {
|
||||
public requestBody: RequestBody;
|
||||
@@ -56,7 +55,7 @@ export class DiscordAPIError extends Error {
|
||||
public status: number,
|
||||
public method: string,
|
||||
public url: string,
|
||||
bodyData: Pick<InternalRequest, 'files' | 'body'>,
|
||||
bodyData: Pick<InternalRequest, 'body' | 'files'>,
|
||||
) {
|
||||
super(DiscordAPIError.getMessage(rawError));
|
||||
|
||||
@@ -76,31 +75,40 @@ export class DiscordAPIError extends 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';
|
||||
}
|
||||
|
||||
// eslint-disable-next-line consistent-return
|
||||
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;
|
||||
for (const [otherKey, val] of Object.entries(obj)) {
|
||||
const nextKey = otherKey.startsWith('_')
|
||||
? key
|
||||
: key
|
||||
? Number.isNaN(Number(otherKey))
|
||||
? `${key}.${otherKey}`
|
||||
: `${key}[${otherKey}]`
|
||||
: otherKey;
|
||||
|
||||
if (typeof v === 'string') {
|
||||
yield v;
|
||||
if (typeof val === 'string') {
|
||||
yield val;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
} else if (isErrorGroupWrapper(v)) {
|
||||
for (const error of v._errors) {
|
||||
} else if (isErrorGroupWrapper(val)) {
|
||||
for (const error of val._errors) {
|
||||
yield* this.flattenDiscordError(error, nextKey);
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
yield* this.flattenDiscordError(v, nextKey);
|
||||
yield* this.flattenDiscordError(val, nextKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { RequestBody } from './DiscordAPIError';
|
||||
import type { InternalRequest } from '../RequestManager';
|
||||
import type { InternalRequest } from '../RequestManager.js';
|
||||
import type { RequestBody } from './DiscordAPIError.js';
|
||||
|
||||
/**
|
||||
* Represents a HTTP error
|
||||
@@ -19,7 +19,7 @@ export class HTTPError extends Error {
|
||||
public status: number,
|
||||
public method: string,
|
||||
public url: string,
|
||||
bodyData: Pick<InternalRequest, 'files' | 'body'>,
|
||||
bodyData: Pick<InternalRequest, 'body' | 'files'>,
|
||||
) {
|
||||
super();
|
||||
|
||||
|
||||
@@ -2,13 +2,21 @@ 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;
|
||||
|
||||
Reference in New Issue
Block a user