fix(errors): error codes (#8398)

This commit is contained in:
DD
2022-07-30 21:49:59 +03:00
committed by GitHub
parent b45b99f92b
commit 480c85c9c3
3 changed files with 222 additions and 227 deletions

View File

@@ -3,7 +3,6 @@
// Heavily inspired by node's `internal/errors` module // Heavily inspired by node's `internal/errors` module
const ErrorCodes = require('./ErrorCodes'); const ErrorCodes = require('./ErrorCodes');
const Messages = require('./Messages'); const Messages = require('./Messages');
const kCode = Symbol('code');
/** /**
* Extend an error of some sort into a DiscordjsError. * Extend an error of some sort into a DiscordjsError.
@@ -15,17 +14,13 @@ function makeDiscordjsError(Base) {
return class DiscordjsError extends Base { return class DiscordjsError extends Base {
constructor(code, ...args) { constructor(code, ...args) {
super(message(code, args)); super(message(code, args));
this[kCode] = code; this.code = code;
Error.captureStackTrace?.(this, DiscordjsError); Error.captureStackTrace?.(this, DiscordjsError);
} }
get name() { get name() {
return `${super.name} [${this.code}]`; return `${super.name} [${this.code}]`;
} }
get code() {
return ErrorCodes[this[kCode]];
}
}; };
} }
@@ -37,9 +32,9 @@ function makeDiscordjsError(Base) {
* @ignore * @ignore
*/ */
function message(code, args) { function message(code, args) {
if (typeof code !== 'number') throw new Error('Error code must be a valid DiscordjsErrorCodes'); if (!(code in ErrorCodes)) throw new Error('Error code must be a valid DiscordjsErrorCodes');
const msg = Messages[code]; const msg = Messages[code];
if (!msg) throw new Error(`An invalid error code was used: ${code}.`); if (!msg) throw new Error(`No message associated with error code: ${code}.`);
if (typeof msg === 'function') return msg(...args); if (typeof msg === 'function') return msg(...args);
if (!args?.length) return msg; if (!args?.length) return msg;
args.unshift(msg); args.unshift(msg);

View File

@@ -1,159 +1,152 @@
'use strict'; 'use strict';
const { createEnum } = require('../util/Enums');
/** /**
* @typedef {Object} DiscordjsErrorCodes * @typedef {Object} DiscordjsErrorCodes
* @property {number} ClientInvalidOption * @property {'ClientInvalidOption'} ClientInvalidOption
* @property {number} ClientInvalidProvidedShards * @property {'ClientInvalidProvidedShards'} ClientInvalidProvidedShards
* @property {number} ClientMissingIntents * @property {'ClientMissingIntents'} ClientMissingIntents
* @property {number} ClientNotReady * @property {'ClientNotReady'} ClientNotReady
* @property {number} TokenInvalid * @property {'TokenInvalid'} TokenInvalid
* @property {number} TokenMissing * @property {'TokenMissing'} TokenMissing
* @property {number} ApplicationCommandPermissionsTokenMissing * @property {'ApplicationCommandPermissionsTokenMissing'} ApplicationCommandPermissionsTokenMissing
* @property {number} WSCloseRequested * @property {'WSCloseRequested'} WSCloseRequested
* @property {number} WSConnectionExists * @property {'WSConnectionExists'} WSConnectionExists
* @property {number} WSNotOpen * @property {'WSNotOpen'} WSNotOpen
* @property {number} ManagerDestroyed * @property {'ManagerDestroyed'} ManagerDestroyed
* @property {number} BitFieldInvalid * @property {'BitFieldInvalid'} BitFieldInvalid
* @property {number} ShardingInvalid * @property {'ShardingInvalid'} ShardingInvalid
* @property {number} ShardingRequired * @property {'ShardingRequired'} ShardingRequired
* @property {number} InvalidIntents * @property {'InvalidIntents'} InvalidIntents
* @property {number} DisallowedIntents * @property {'DisallowedIntents'} DisallowedIntents
* @property {number} ShardingNoShards * @property {'ShardingNoShards'} ShardingNoShards
* @property {number} ShardingInProcess * @property {'ShardingInProcess'} ShardingInProcess
* @property {number} ShardingInvalidEvalBroadcast * @property {'ShardingInvalidEvalBroadcast'} ShardingInvalidEvalBroadcast
* @property {number} ShardingShardNotFound * @property {'ShardingShardNotFound'} ShardingShardNotFound
* @property {number} ShardingAlreadySpawned * @property {'ShardingAlreadySpawned'} ShardingAlreadySpawned
* @property {number} ShardingProcessExists * @property {'ShardingProcessExists'} ShardingProcessExists
* @property {number} ShardingWorkerExists * @property {'ShardingWorkerExists'} ShardingWorkerExists
* @property {number} ShardingReadyTimeout * @property {'ShardingReadyTimeout'} ShardingReadyTimeout
* @property {number} ShardingReadyDisconnected * @property {'ShardingReadyDisconnected'} ShardingReadyDisconnected
* @property {number} ShardingReadyDied * @property {'ShardingReadyDied'} ShardingReadyDied
* @property {number} ShardingNoChildExists * @property {'ShardingNoChildExists'} ShardingNoChildExists
* @property {number} ShardingShardMiscalculation * @property {'ShardingShardMiscalculation'} ShardingShardMiscalculation
* @property {number} ColorRange * @property {'ColorRange'} ColorRange
* @property {number} ColorConvert * @property {'ColorConvert'} ColorConvert
* @property {number} InviteOptionsMissingChannel * @property {'InviteOptionsMissingChannel'} InviteOptionsMissingChannel
* @property {number} ButtonLabel * @property {'ButtonLabel'} ButtonLabel
* @property {number} ButtonURL * @property {'ButtonURL'} ButtonURL
* @property {number} ButtonCustomId * @property {'ButtonCustomId'} ButtonCustomId
* @property {number} SelectMenuCustomId * @property {'SelectMenuCustomId'} SelectMenuCustomId
* @property {number} SelectMenuPlaceholder * @property {'SelectMenuPlaceholder'} SelectMenuPlaceholder
* @property {number} SelectOptionLabel * @property {'SelectOptionLabel'} SelectOptionLabel
* @property {number} SelectOptionValue * @property {'SelectOptionValue'} SelectOptionValue
* @property {number} SelectOptionDescription * @property {'SelectOptionDescription'} SelectOptionDescription
* @property {number} InteractionCollectorError * @property {'InteractionCollectorError'} InteractionCollectorError
* @property {number} FileNotFound * @property {'FileNotFound'} FileNotFound
* @property {number} UserBannerNotFetched * @property {'UserBannerNotFetched'} UserBannerNotFetched
* @property {number} UserNoDMChannel * @property {'UserNoDMChannel'} UserNoDMChannel
* @property {number} VoiceNotStageChannel * @property {'VoiceNotStageChannel'} VoiceNotStageChannel
* @property {number} VoiceStateNotOwn * @property {'VoiceStateNotOwn'} VoiceStateNotOwn
* @property {number} VoiceStateInvalidType * @property {'VoiceStateInvalidType'} VoiceStateInvalidType
* @property {number} ReqResourceType * @property {'ReqResourceType'} ReqResourceType
* @property {number} ImageFormat * @property {'ImageFormat'} ImageFormat
* @property {number} ImageSize * @property {'ImageSize'} ImageSize
* @property {number} MessageBulkDeleteType * @property {'MessageBulkDeleteType'} MessageBulkDeleteType
* @property {number} MessageNonceType * @property {'MessageNonceType'} MessageNonceType
* @property {number} MessageContentType * @property {'MessageContentType'} MessageContentType
* @property {number} SplitMaxLen * @property {'SplitMaxLen'} SplitMaxLen
* @property {number} BanResolveId * @property {'BanResolveId'} BanResolveId
* @property {number} FetchBanResolveId * @property {'FetchBanResolveId'} FetchBanResolveId
* @property {number} PruneDaysType * @property {'PruneDaysType'} PruneDaysType
* @property {number} GuildChannelResolve * @property {'GuildChannelResolve'} GuildChannelResolve
* @property {number} GuildVoiceChannelResolve * @property {'GuildVoiceChannelResolve'} GuildVoiceChannelResolve
* @property {number} GuildChannelOrphan * @property {'GuildChannelOrphan'} GuildChannelOrphan
* @property {number} GuildChannelUnowned * @property {'GuildChannelUnowned'} GuildChannelUnowned
* @property {number} GuildOwned * @property {'GuildOwned'} GuildOwned
* @property {number} GuildMembersTimeout * @property {'GuildMembersTimeout'} GuildMembersTimeout
* @property {number} GuildUncachedMe * @property {'GuildUncachedMe'} GuildUncachedMe
* @property {number} ChannelNotCached * @property {'ChannelNotCached'} ChannelNotCached
* @property {number} StageChannelResolve * @property {'StageChannelResolve'} StageChannelResolve
* @property {number} GuildScheduledEventResolve * @property {'GuildScheduledEventResolve'} GuildScheduledEventResolve
* @property {number} FetchOwnerId * @property {'FetchOwnerId'} FetchOwnerId
* @property {number} InvalidType * @property {'InvalidType'} InvalidType
* @property {number} InvalidElement * @property {'InvalidElement'} InvalidElement
* @property {number} MessageThreadParent * @property {'MessageThreadParent'} MessageThreadParent
* @property {number} MessageExistingThread * @property {'MessageExistingThread'} MessageExistingThread
* @property {number} ThreadInvitableType * @property {'ThreadInvitableType'} ThreadInvitableType
* @property {number} WebhookMessage * @property {'WebhookMessage'} WebhookMessage
* @property {number} WebhookTokenUnavailable * @property {'WebhookTokenUnavailable'} WebhookTokenUnavailable
* @property {number} WebhookURLInvalid * @property {'WebhookURLInvalid'} WebhookURLInvalid
* @property {number} WebhookApplication * @property {'WebhookApplication'} WebhookApplication
* @property {number} MessageReferenceMissing * @property {'MessageReferenceMissing'} MessageReferenceMissing
* @property {number} EmojiType * @property {'EmojiType'} EmojiType
* @property {number} EmojiManaged * @property {'EmojiManaged'} EmojiManaged
* @property {number} MissingManageEmojisAndStickersPermission * @property {'MissingManageEmojisAndStickersPermission'} MissingManageEmojisAndStickersPermission
* @property {number} NotGuildSticker * @property {'NotGuildSticker'} NotGuildSticker
* @property {number} ReactionResolveUser * @property {'ReactionResolveUser'} ReactionResolveUser
* @property {number} VanityURL * @property {'VanityURL'} VanityURL
* @property {number} InviteResolveCode * @property {'InviteResolveCode'} InviteResolveCode
* @property {number} InviteNotFound * @property {'InviteNotFound'} InviteNotFound
* @property {number} DeleteGroupDMChannel * @property {'DeleteGroupDMChannel'} DeleteGroupDMChannel
* @property {number} FetchGroupDMChannel * @property {'FetchGroupDMChannel'} FetchGroupDMChannel
* @property {number} MemberFetchNonceLength * @property {'MemberFetchNonceLength'} MemberFetchNonceLength
* @property {number} GlobalCommandPermissions * @property {'GlobalCommandPermissions'} GlobalCommandPermissions
* @property {number} GuildUncachedEntityResolve * @property {'GuildUncachedEntityResolve'} GuildUncachedEntityResolve
* @property {number} InteractionAlreadyReplied * @property {'InteractionAlreadyReplied'} InteractionAlreadyReplied
* @property {number} InteractionNotReplied * @property {'InteractionNotReplied'} InteractionNotReplied
* @property {number} InteractionEphemeralReplied * @property {'InteractionEphemeralReplied'} InteractionEphemeralReplied
* @property {number} CommandInteractionOptionNotFound * @property {'CommandInteractionOptionNotFound'} CommandInteractionOptionNotFound
* @property {number} CommandInteractionOptionType * @property {'CommandInteractionOptionType'} CommandInteractionOptionType
* @property {number} CommandInteractionOptionEmpty * @property {'CommandInteractionOptionEmpty'} CommandInteractionOptionEmpty
* @property {number} CommandInteractionOptionNoSubcommand * @property {'CommandInteractionOptionNoSubcommand'} CommandInteractionOptionNoSubcommand
* @property {number} CommandInteractionOptionNoSubcommandGroup * @property {'CommandInteractionOptionNoSubcommandGroup'} CommandInteractionOptionNoSubcommandGroup
* @property {number} AutocompleteInteractionOptionNoFocusedOption * @property {'AutocompleteInteractionOptionNoFocusedOption'} AutocompleteInteractionOptionNoFocusedOption
* @property {number} ModalSubmitInteractionFieldNotFound * @property {'ModalSubmitInteractionFieldNotFound'} ModalSubmitInteractionFieldNotFound
* @property {number} ModalSubmitInteractionFieldType * @property {'ModalSubmitInteractionFieldType'} ModalSubmitInteractionFieldType
* @property {number} InvalidMissingScopes * @property {'InvalidMissingScopes'} InvalidMissingScopes
* @property {number} NotImplemented * @property {'NotImplemented'} NotImplemented
* @property {number} SweepFilterReturn * @property {'SweepFilterReturn'} SweepFilterReturn
*/ */
// JSDoc for IntelliSense purposes const keys = [
/**
* @type {DiscordjsErrorCodes}
* @ignore
*/
module.exports = createEnum([
'ClientInvalidOption', 'ClientInvalidOption',
'ClientInvalidProvidedShards', 'ClientInvalidProvidedShards',
'ClientMissingIntents', 'ClientMissingIntents',
@@ -295,4 +288,11 @@ module.exports = createEnum([
'NotImplemented', 'NotImplemented',
'SweepFilterReturn', 'SweepFilterReturn',
]); ];
// JSDoc for IntelliSense purposes
/**
* @type {DiscordjsErrorCodes}
* @ignore
*/
module.exports = Object.fromEntries(keys.map(key => [key, key]));

View File

@@ -3021,152 +3021,152 @@ export const version: string;
//#region Errors //#region Errors
export enum DiscordjsErrorCodes { export enum DiscordjsErrorCodes {
ClientInvalidOption, ClientInvalidOption = 'ClientInvalidOption',
ClientInvalidProvidedShards, ClientInvalidProvidedShards = 'ClientInvalidProvidedShards',
ClientMissingIntents, ClientMissingIntents = 'ClientMissingIntents',
ClientNotReady, ClientNotReady = 'ClientNotReady',
TokenInvalid, TokenInvalid = 'TokenInvalid',
TokenMissing, TokenMissing = 'TokenMissing',
ApplicationCommandPermissionsTokenMissing, ApplicationCommandPermissionsTokenMissing = 'ApplicationCommandPermissionsTokenMissing',
WSCloseRequested, WSCloseRequested = 'WSCloseRequested',
WSConnectionExists, WSConnectionExists = 'WSConnectionExists',
WSNotOpen, WSNotOpen = 'WSNotOpen',
ManagerDestroyed, ManagerDestroyed = 'ManagerDestroyed',
BitFieldInvalid, BitFieldInvalid = 'BitFieldInvalid',
ShardingInvalid, ShardingInvalid = 'ShardingInvalid',
ShardingRequired, ShardingRequired = 'ShardingRequired',
InvalidIntents, InvalidIntents = 'InvalidIntents',
DisallowedIntents, DisallowedIntents = 'DisallowedIntents',
ShardingNoShards, ShardingNoShards = 'ShardingNoShards',
ShardingInProcess, ShardingInProcess = 'ShardingInProcess',
ShardingInvalidEvalBroadcast, ShardingInvalidEvalBroadcast = 'ShardingInvalidEvalBroadcast',
ShardingShardNotFound, ShardingShardNotFound = 'ShardingShardNotFound',
ShardingAlreadySpawned, ShardingAlreadySpawned = 'ShardingAlreadySpawned',
ShardingProcessExists, ShardingProcessExists = 'ShardingProcessExists',
ShardingWorkerExists, ShardingWorkerExists = 'ShardingWorkerExists',
ShardingReadyTimeout, ShardingReadyTimeout = 'ShardingReadyTimeout',
ShardingReadyDisconnected, ShardingReadyDisconnected = 'ShardingReadyDisconnected',
ShardingReadyDied, ShardingReadyDied = 'ShardingReadyDied',
ShardingNoChildExists, ShardingNoChildExists = 'ShardingNoChildExists',
ShardingShardMiscalculation, ShardingShardMiscalculation = 'ShardingShardMiscalculation',
ColorRange, ColorRange = 'ColorRange',
ColorConvert, ColorConvert = 'ColorConvert',
InviteOptionsMissingChannel, InviteOptionsMissingChannel = 'InviteOptionsMissingChannel',
ButtonLabel, ButtonLabel = 'ButtonLabel',
ButtonURL, ButtonURL = 'ButtonURL',
ButtonCustomId, ButtonCustomId = 'ButtonCustomId',
SelectMenuCustomId, SelectMenuCustomId = 'SelectMenuCustomId',
SelectMenuPlaceholder, SelectMenuPlaceholder = 'SelectMenuPlaceholder',
SelectOptionLabel, SelectOptionLabel = 'SelectOptionLabel',
SelectOptionValue, SelectOptionValue = 'SelectOptionValue',
SelectOptionDescription, SelectOptionDescription = 'SelectOptionDescription',
InteractionCollectorError, InteractionCollectorError = 'InteractionCollectorError',
FileNotFound, FileNotFound = 'FileNotFound',
UserBannerNotFetched, UserBannerNotFetched = 'UserBannerNotFetched',
UserNoDMChannel, UserNoDMChannel = 'UserNoDMChannel',
VoiceNotStageChannel, VoiceNotStageChannel = 'VoiceNotStageChannel',
VoiceStateNotOwn, VoiceStateNotOwn = 'VoiceStateNotOwn',
VoiceStateInvalidType, VoiceStateInvalidType = 'VoiceStateInvalidType',
ReqResourceType, ReqResourceType = 'ReqResourceType',
ImageFormat, ImageFormat = 'ImageFormat',
ImageSize, ImageSize = 'ImageSize',
MessageBulkDeleteType, MessageBulkDeleteType = 'MessageBulkDeleteType',
MessageNonceType, MessageNonceType = 'MessageNonceType',
MessageContentType, MessageContentType = 'MessageContentType',
SplitMaxLen, SplitMaxLen = 'SplitMaxLen',
BanResolveId, BanResolveId = 'BanResolveId',
FetchBanResolveId, FetchBanResolveId = 'FetchBanResolveId',
PruneDaysType, PruneDaysType = 'PruneDaysType',
GuildChannelResolve, GuildChannelResolve = 'GuildChannelResolve',
GuildVoiceChannelResolve, GuildVoiceChannelResolve = 'GuildVoiceChannelResolve',
GuildChannelOrphan, GuildChannelOrphan = 'GuildChannelOrphan',
GuildChannelUnowned, GuildChannelUnowned = 'GuildChannelUnowned',
GuildOwned, GuildOwned = 'GuildOwned',
GuildMembersTimeout, GuildMembersTimeout = 'GuildMembersTimeout',
GuildUncachedMe, GuildUncachedMe = 'GuildUncachedMe',
ChannelNotCached, ChannelNotCached = 'ChannelNotCached',
StageChannelResolve, StageChannelResolve = 'StageChannelResolve',
GuildScheduledEventResolve, GuildScheduledEventResolve = 'GuildScheduledEventResolve',
FetchOwnerId, FetchOwnerId = 'FetchOwnerId',
InvalidType, InvalidType = 'InvalidType',
InvalidElement, InvalidElement = 'InvalidElement',
MessageThreadParent, MessageThreadParent = 'MessageThreadParent',
MessageExistingThread, MessageExistingThread = 'MessageExistingThread',
ThreadInvitableType, ThreadInvitableType = 'ThreadInvitableType',
WebhookMessage, WebhookMessage = 'WebhookMessage',
WebhookTokenUnavailable, WebhookTokenUnavailable = 'WebhookTokenUnavailable',
WebhookURLInvalid, WebhookURLInvalid = 'WebhookURLInvalid',
WebhookApplication, WebhookApplication = 'WebhookApplication',
MessageReferenceMissing, MessageReferenceMissing = 'MessageReferenceMissing',
EmojiType, EmojiType = 'EmojiType',
EmojiManaged, EmojiManaged = 'EmojiManaged',
MissingManageEmojisAndStickersPermission, MissingManageEmojisAndStickersPermission = 'MissingManageEmojisAndStickersPermission',
NotGuildSticker, NotGuildSticker = 'NotGuildSticker',
ReactionResolveUser, ReactionResolveUser = 'ReactionResolveUser',
VanityURL, VanityURL = 'VanityURL',
InviteResolveCode, InviteResolveCode = 'InviteResolveCode',
InviteNotFound, InviteNotFound = 'InviteNotFound',
DeleteGroupDMChannel, DeleteGroupDMChannel = 'DeleteGroupDMChannel',
FetchGroupDMChannel, FetchGroupDMChannel = 'FetchGroupDMChannel',
MemberFetchNonceLength, MemberFetchNonceLength = 'MemberFetchNonceLength',
GlobalCommandPermissions, GlobalCommandPermissions = 'GlobalCommandPermissions',
GuildUncachedEntityResolve, GuildUncachedEntityResolve = 'GuildUncachedEntityResolve',
InteractionAlreadyReplied, InteractionAlreadyReplied = 'InteractionAlreadyReplied',
InteractionNotReplied, InteractionNotReplied = 'InteractionNotReplied',
InteractionEphemeralReplied, InteractionEphemeralReplied = 'InteractionEphemeralReplied',
CommandInteractionOptionNotFound, CommandInteractionOptionNotFound = 'CommandInteractionOptionNotFound',
CommandInteractionOptionType, CommandInteractionOptionType = 'CommandInteractionOptionType',
CommandInteractionOptionEmpty, CommandInteractionOptionEmpty = 'CommandInteractionOptionEmpty',
CommandInteractionOptionNoSubcommand, CommandInteractionOptionNoSubcommand = 'CommandInteractionOptionNoSubcommand',
CommandInteractionOptionNoSubcommandGroup, CommandInteractionOptionNoSubcommandGroup = 'CommandInteractionOptionNoSubcommandGroup',
AutocompleteInteractionOptionNoFocusedOption, AutocompleteInteractionOptionNoFocusedOption = 'AutocompleteInteractionOptionNoFocusedOption',
ModalSubmitInteractionFieldNotFound, ModalSubmitInteractionFieldNotFound = 'ModalSubmitInteractionFieldNotFound',
ModalSubmitInteractionFieldType, ModalSubmitInteractionFieldType = 'ModalSubmitInteractionFieldType',
InvalidMissingScopes, InvalidMissingScopes = 'InvalidMissingScopes',
NotImplemented, NotImplemented = 'NotImplemented',
SweepFilterReturn, SweepFilterReturn = 'SweepFilterReturn',
} }
export interface DiscordjsErrorFields<Name extends string> { export interface DiscordjsErrorFields<Name extends string> {
readonly name: `${Name} [${keyof typeof DiscordjsErrorCodes}]`; readonly name: `${Name} [${DiscordjsErrorCodes}]`;
get code(): keyof typeof DiscordjsErrorCodes; get code(): DiscordjsErrorCodes;
} }
export function DiscordjsErrorMixin<T, N extends string>( export function DiscordjsErrorMixin<T, N extends string>(