Skip to content

Commit cad95d5

Browse files
feat(Error): port DJS error module with TypeScript compatibility
1 parent 9f7d804 commit cad95d5

File tree

3 files changed

+431
-0
lines changed

3 files changed

+431
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// reference => https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/errors/DJSError.js
2+
3+
import { DiscordHttpsErrorCodes } from "./ErrorCode.js";
4+
import { Messages } from "./ErrorMessage.js";
5+
6+
/**
7+
* Extend an error of some sort into a DiscordHttpsError.
8+
*
9+
* @param {Error} Base Base error to extend
10+
* @ignore
11+
*/
12+
function makeDiscordHttpsError<T extends new (...a: any[]) => Error>(Base: T) {
13+
return class DiscordHttpsError extends Base {
14+
public readonly code: DiscordHttpsErrorCodes;
15+
// A mixin class must have a constructor with a single rest parameter of type 'any[]'
16+
constructor(...[code, ...args]: any[]) {
17+
super(formatMessage(code as DiscordHttpsErrorCodes, args));
18+
this.code = code;
19+
Error.captureStackTrace?.(this, DiscordHttpsError);
20+
}
21+
22+
override get name(): string {
23+
return `${super.name} [${this.code}]`;
24+
}
25+
};
26+
}
27+
28+
/**
29+
* Format the message for an error.
30+
*
31+
* @param code The error code
32+
* @param args Arguments to pass for util format or as function args
33+
* @returns Formatted string
34+
* @ignore
35+
*/
36+
37+
function formatMessage(code: DiscordHttpsErrorCodes, args: any[]): string {
38+
if (!(code in DiscordHttpsErrorCodes)) {
39+
throw new Error("Error code must be a valid DiscordHttpsErrorCodes");
40+
}
41+
const msg = Messages[code];
42+
if (!msg) {
43+
throw new Error(`No message associated with error code: ${code}.`);
44+
}
45+
if (typeof msg === "function") return msg(...args);
46+
if (!args?.length) return msg;
47+
args.unshift(msg);
48+
return String(...args);
49+
}
50+
51+
export const DiscordHttpsError = makeDiscordHttpsError(Error);
52+
export const DiscordjsTypeError = makeDiscordHttpsError(TypeError);
53+
export const DiscordjsRangeError = makeDiscordHttpsError(RangeError);

src/structures/errors/ErrorCode.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// reference => https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/errors/ErrorCodes.js
2+
3+
/**
4+
* Enum of DiscordHttps error codes.
5+
*/
6+
7+
export enum DiscordHttpsErrorCodes {
8+
ClientInvalidOption = "ClientInvalidOption",
9+
ClientInvalidProvidedShards = "ClientInvalidProvidedShards",
10+
ClientMissingIntents = "ClientMissingIntents",
11+
ClientNotReady = "ClientNotReady",
12+
13+
TokenInvalid = "TokenInvalid",
14+
TokenMissing = "TokenMissing",
15+
ApplicationCommandPermissionsTokenMissing = "ApplicationCommandPermissionsTokenMissing",
16+
17+
BitFieldInvalid = "BitFieldInvalid",
18+
19+
ShardingNoShards = "ShardingNoShards",
20+
ShardingInProcess = "ShardingInProcess",
21+
ShardingInvalidEvalBroadcast = "ShardingInvalidEvalBroadcast",
22+
ShardingShardNotFound = "ShardingShardNotFound",
23+
ShardingAlreadySpawned = "ShardingAlreadySpawned",
24+
ShardingProcessExists = "ShardingProcessExists",
25+
ShardingWorkerExists = "ShardingWorkerExists",
26+
ShardingReadyTimeout = "ShardingReadyTimeout",
27+
ShardingReadyDisconnected = "ShardingReadyDisconnected",
28+
ShardingReadyDied = "ShardingReadyDied",
29+
ShardingNoChildExists = "ShardingNoChildExists",
30+
ShardingShardMiscalculation = "ShardingShardMiscalculation",
31+
32+
ColorRange = "ColorRange",
33+
ColorConvert = "ColorConvert",
34+
35+
InviteOptionsMissingChannel = "InviteOptionsMissingChannel",
36+
37+
InteractionCollectorError = "InteractionCollectorError",
38+
39+
FileNotFound = "FileNotFound",
40+
41+
UserNoDMChannel = "UserNoDMChannel",
42+
43+
VoiceNotStageChannel = "VoiceNotStageChannel",
44+
VoiceStateNotOwn = "VoiceStateNotOwn",
45+
VoiceStateInvalidType = "VoiceStateInvalidType",
46+
47+
ReqResourceType = "ReqResourceType",
48+
49+
MessageBulkDeleteType = "MessageBulkDeleteType",
50+
MessageContentType = "MessageContentType",
51+
MessageNonceRequired = "MessageNonceRequired",
52+
MessageNonceType = "MessageNonceType",
53+
54+
BanResolveId = "BanResolveId",
55+
FetchBanResolveId = "FetchBanResolveId",
56+
57+
PruneDaysType = "PruneDaysType",
58+
59+
GuildChannelResolve = "GuildChannelResolve",
60+
GuildVoiceChannelResolve = "GuildVoiceChannelResolve",
61+
GuildChannelOrphan = "GuildChannelOrphan",
62+
GuildChannelUnowned = "GuildChannelUnowned",
63+
GuildOwned = "GuildOwned",
64+
GuildMembersTimeout = "GuildMembersTimeout",
65+
GuildSoundboardSoundsTimeout = "GuildSoundboardSoundsTimeout",
66+
GuildUncachedMe = "GuildUncachedMe",
67+
ChannelNotCached = "ChannelNotCached",
68+
StageChannelResolve = "StageChannelResolve",
69+
GuildScheduledEventResolve = "GuildScheduledEventResolve",
70+
FetchOwnerId = "FetchOwnerId",
71+
72+
InvalidType = "InvalidType",
73+
InvalidElement = "InvalidElement",
74+
75+
MessageThreadParent = "MessageThreadParent",
76+
MessageExistingThread = "MessageExistingThread",
77+
ThreadInvitableType = "ThreadInvitableType",
78+
NotAThreadOfParent = "NotAThreadOfParent",
79+
80+
WebhookMessage = "WebhookMessage",
81+
WebhookTokenUnavailable = "WebhookTokenUnavailable",
82+
WebhookURLInvalid = "WebhookURLInvalid",
83+
WebhookApplication = "WebhookApplication",
84+
85+
MessageReferenceMissing = "MessageReferenceMissing",
86+
87+
EmojiType = "EmojiType",
88+
EmojiManaged = "EmojiManaged",
89+
MissingManageGuildExpressionsPermission = "MissingManageGuildExpressionsPermission",
90+
91+
NotGuildSoundboardSound = "NotGuildSoundboardSound",
92+
NotGuildSticker = "NotGuildSticker",
93+
94+
ReactionResolveUser = "ReactionResolveUser",
95+
96+
InviteResolveCode = "InviteResolveCode",
97+
InviteNotFound = "InviteNotFound",
98+
99+
DeleteGroupDMChannel = "DeleteGroupDMChannel",
100+
FetchGroupDMChannel = "FetchGroupDMChannel",
101+
102+
MemberFetchNonceLength = "MemberFetchNonceLength",
103+
104+
GlobalCommandPermissions = "GlobalCommandPermissions",
105+
GuildUncachedEntityResolve = "GuildUncachedEntityResolve",
106+
107+
InteractionAlreadyReplied = "InteractionAlreadyReplied",
108+
InteractionNotReplied = "InteractionNotReplied",
109+
110+
CommandInteractionOptionNotFound = "CommandInteractionOptionNotFound",
111+
CommandInteractionOptionType = "CommandInteractionOptionType",
112+
CommandInteractionOptionEmpty = "CommandInteractionOptionEmpty",
113+
CommandInteractionOptionNoSubcommand = "CommandInteractionOptionNoSubcommand",
114+
CommandInteractionOptionNoSubcommandGroup = "CommandInteractionOptionNoSubcommandGroup",
115+
CommandInteractionOptionInvalidChannelType = "CommandInteractionOptionInvalidChannelType",
116+
AutocompleteInteractionOptionNoFocusedOption = "AutocompleteInteractionOptionNoFocusedOption",
117+
118+
ModalSubmitInteractionFieldNotFound = "ModalSubmitInteractionFieldNotFound",
119+
ModalSubmitInteractionFieldType = "ModalSubmitInteractionFieldType",
120+
121+
InvalidMissingScopes = "InvalidMissingScopes",
122+
InvalidScopesWithPermissions = "InvalidScopesWithPermissions",
123+
124+
NotImplemented = "NotImplemented",
125+
126+
SweepFilterReturn = "SweepFilterReturn",
127+
128+
GuildForumMessageRequired = "GuildForumMessageRequired",
129+
130+
EntitlementCreateInvalidOwner = "EntitlementCreateInvalidOwner",
131+
132+
BulkBanUsersOptionEmpty = "BulkBanUsersOptionEmpty",
133+
134+
PollAlreadyExpired = "PollAlreadyExpired",
135+
136+
PermissionOverwritesTypeMandatory = "PermissionOverwritesTypeMandatory",
137+
PermissionOverwritesTypeMismatch = "PermissionOverwritesTypeMismatch",
138+
}

0 commit comments

Comments
 (0)