Skip to content

Commit ce05277

Browse files
authored
Add enum for JSON error codes in DiscordJsonError (#2964)
Fixes #2752
1 parent 1db2076 commit ce05277

File tree

1 file changed

+238
-3
lines changed

1 file changed

+238
-3
lines changed

src/http/error.rs

Lines changed: 238 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,246 @@ use url::ParseError as UrlError;
1010

1111
use crate::internal::prelude::*;
1212

13+
enum_number! {
14+
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
15+
#[non_exhaustive]
16+
pub enum JsonErrorCode {
17+
General = 0,
18+
19+
// Unknown entity (10xxx)
20+
UnknownAccount = 10001,
21+
UnknownApplication = 10002,
22+
UnknownChannel = 10003,
23+
UnknownGuild = 10004,
24+
UnknownIntegration = 10005,
25+
UnknownInvite = 10006,
26+
UnknownMember = 10007,
27+
UnknownMessage = 10008,
28+
UnknownPermissionOverwrite = 10009,
29+
UnknownProvider = 10010,
30+
UnknownRole = 10011,
31+
UnknownToken = 10012,
32+
UnknownUser = 10013,
33+
UnknownEmoji = 10014,
34+
UnknownWebhook = 10015,
35+
UnknownWebhookService = 10016,
36+
UnknownSession = 10020,
37+
UnknownBan = 10026,
38+
UnknownSKU = 10027,
39+
UnknownStoreListing = 10028,
40+
UnknownEntitlement = 10029,
41+
UnknownBuild = 10030,
42+
UnknownLobby = 10031,
43+
UnknownBranch = 10032,
44+
UnknownStoreDirectoryLayout = 10033,
45+
UnknownRedistributable = 10036,
46+
UnknownGiftCode = 10038,
47+
UnknownStream = 10049,
48+
UnknownPremiumServerSubscribeCooldown = 10050,
49+
UnknownGuildTemplate = 10057,
50+
UnknownDiscoverableServerCategory = 10059,
51+
UnknownSticker = 10060,
52+
UnknownStickerPack = 10061,
53+
UnknownInteraction = 10062,
54+
UnknownApplicationCommand = 10063,
55+
UnknownVoiceState = 10065,
56+
UnknownApplicationCommandPermissions = 10066,
57+
UnknownStageInstance = 10067,
58+
UnknownGuildMemberVerificationForm = 10068,
59+
UnknownGuildWelcomeScreen = 10069,
60+
UnknownGuildScheduledEvent = 10070,
61+
UnknownGuildScheduledEventUser = 10071,
62+
UnknownTag = 10087,
63+
64+
// Hit restriction (20xxx)
65+
BotsCannotUseThisEndpoint = 20001,
66+
OnlyBotsCanUseThisEndpoint = 20002,
67+
ExplicitContentCannotBeSentToRecipient = 20009,
68+
NotAuthorizedForAction = 20012,
69+
SlowmodeRateLimit = 20016,
70+
OnlyOwnerCanPerform = 20018,
71+
AnnouncementRateLimit = 20022,
72+
UnderMinimumAge = 20024,
73+
ChannelWriteRateLimit = 20028,
74+
ServerWriteRateLimit = 20029,
75+
ForbiddenWordsInName = 20031,
76+
GuildPremiumSubscriptionLevelTooLow = 20035,
77+
78+
// Hit maximum limit (30xxx)
79+
MaxGuildsReached = 30001,
80+
MaxFriendsReached = 30002,
81+
MaxPinsReached = 30003,
82+
MaxRecipientsReached = 30004,
83+
MaxGuildRolesReached = 30005,
84+
MaxWebhooksReached = 30007,
85+
MaxEmojisReached = 30008,
86+
MaxReactionsReached = 30010,
87+
MaxGroupDMsReached = 30011,
88+
MaxGuildChannelsReached = 30013,
89+
MaxAttachmentsReached = 30015,
90+
MaxInvitesReached = 30016,
91+
MaxAnimatedEmojisReached = 30018,
92+
MaxServerMembersReached = 30019,
93+
MaxServerCategoriesReached = 30030,
94+
GuildAlreadyHasTemplate = 30031,
95+
MaxApplicationCommandsReached = 30032,
96+
MaxThreadParticipantsReached = 30033,
97+
MaxDailyApplicationCommandCreatesReached = 30034,
98+
MaxNonGuildMemberBansExceeded = 30035,
99+
MaxBansFetchesReached = 30037,
100+
MaxUncompletedGuildScheduledEventsReached = 30038,
101+
MaxStickersReached = 30039,
102+
MaxPruneRequestsReached = 30040,
103+
MaxGuildWidgetSettingsUpdatesReached = 30042,
104+
MaxEditsToOldMessagesReached = 30046,
105+
MaxPinnedThreadsInForumChannelReached = 30047,
106+
MaxTagsInForumChannelReached = 30048,
107+
BitrateTooHighForChannelType = 30052,
108+
MaxPremiumEmojisReached = 30056,
109+
MaxWebhooksPerGuildReached = 30058,
110+
MaxChannelPermissionOverwritesReached = 30060,
111+
ChannelsTooLargeForGuild = 30061,
112+
113+
Unauthorized = 40001,
114+
AccountVerificationRequired = 40002,
115+
DirectMessagesTooFast = 40003,
116+
SendMessagesDisabled = 40004,
117+
RequestEntityTooLarge = 40005,
118+
FeatureTemporarilyDisabled = 40006,
119+
UserBannedFromGuild = 40007,
120+
ConnectionRevoked = 40012,
121+
TargetUserNotConnectedToVoice = 40032,
122+
MessageAlreadyCrossposted = 40033,
123+
ApplicationCommandNameExists = 40041,
124+
ApplicationInteractionFailed = 40043,
125+
CannotSendInForumChannel = 40058,
126+
InteractionAlreadyAcknowledged = 40060,
127+
TagNamesMustBeUnique = 40061,
128+
ServiceResourceRateLimited = 40062,
129+
NoTagsForNonModerators = 40066,
130+
TagRequiredForForumPost = 40067,
131+
EntitlementAlreadyGranted = 40074,
132+
CloudflareBlockingRequest = 40333,
133+
134+
MissingAccess = 50001,
135+
InvalidAccountType = 50002,
136+
CannotExecuteInDMChannel = 50003,
137+
GuildWidgetDisabled = 50004,
138+
CannotEditOtherUserMessage = 50005,
139+
CannotSendEmptyMessage = 50006,
140+
CannotSendMessagesToUser = 50007,
141+
CannotSendMessagesInNonTextChannel = 50008,
142+
ChannelVerificationLevelTooHigh = 50009,
143+
OAuth2ApplicationNoBot = 50010,
144+
OAuth2ApplicationLimitReached = 50011,
145+
InvalidOAuth2State = 50012,
146+
LackPermissionsForAction = 50013,
147+
InvalidAuthToken = 50014,
148+
NoteTooLong = 50015,
149+
MessageBulkDeleteCountInvalid = 50016,
150+
InvalidMFALevel = 50017,
151+
MessageOnlyPinnedToSentChannel = 50019,
152+
InvalidInviteCode = 50020,
153+
CannotExecuteOnSystemMessage = 50021,
154+
CannotExecuteOnChannelType = 50024,
155+
InvalidOAuth2AccessToken = 50025,
156+
MissingOAuth2Scope = 50026,
157+
InvalidWebhookToken = 50027,
158+
InvalidRole = 50028,
159+
InvalidRecipient = 50033,
160+
MessageTooOldToBulkDelete = 50034,
161+
InvalidFormBody = 50035,
162+
BotNotInGuild = 50036,
163+
InvalidActivityAction = 50039,
164+
InvalidAPIVersion = 50041,
165+
FileTooLarge = 50045,
166+
InvalidFileUploaded = 50046,
167+
CannotSelfRedeemGift = 50054,
168+
InvalidGuild = 50055,
169+
InvalidSku = 50057,
170+
InvalidRequestOrigin = 50067,
171+
InvalidMessageType = 50068,
172+
PaymentSourceRequiredForGift = 50070,
173+
CannotModifySystemWebhook = 50073,
174+
CannotDeleteCommunityGuildChannel = 50074,
175+
CannotEditStickersInMessage = 50080,
176+
InvalidStickerSent = 50081,
177+
OperationOnArchivedThread = 50083,
178+
InvalidThreadNotificationSettings = 50084,
179+
BeforeValueEarlierThanThreadCreation = 50085,
180+
CommunityServerChannelsMustBeText = 50086,
181+
EventEntityTypeMismatch = 50091,
182+
ServerNotAvailableInLocation = 50095,
183+
ServerMonetizationRequired = 50097,
184+
MoreBoostsRequired = 50101,
185+
InvalidJsonInRequestBody = 50109,
186+
OwnerCannotBePendingMember = 50131,
187+
OwnershipTransferNotAllowedToBot = 50132,
188+
FailedToResizeAsset = 50138,
189+
CannotMixPremiumAndNormalEmoji = 50144,
190+
UploadedFileNotFound = 50146,
191+
VoiceMessagesNoAdditionalContent = 50159,
192+
SingleAudioAttachmentRequired = 50160,
193+
MetadataRequiredForVoiceMessages = 50161,
194+
VoiceMessagesCannotBeEdited = 50162,
195+
CannotDeleteGuildSubscriptionIntegration = 50163,
196+
CannotSendVoiceMessagesInChannel = 50173,
197+
UserAccountMustBeVerified = 50178,
198+
NoPermissionForSticker = 50600,
199+
200+
TwoFactorRequired = 60003,
201+
NoUsersWithDiscordTag = 80004,
202+
ReactionBlocked = 90001,
203+
UserCannotUseBurstReactions = 90002,
204+
205+
ApplicationNotAvailable = 110001,
206+
ApiResourceOverloaded = 130000,
207+
StageAlreadyOpen = 150006,
208+
209+
CannotReplyWithoutPermission = 160002,
210+
ThreadAlreadyCreatedForMessage = 160004,
211+
ThreadLocked = 160005,
212+
MaxActiveThreadsReached = 160006,
213+
MaxActiveAnnouncementThreadsReached = 160007,
214+
215+
InvalidJsonForLottieFile = 170001,
216+
LottiesCannotContainRasterizedImages = 170002,
217+
StickerMaxFramerateExceeded = 170003,
218+
StickerFrameCountExceedsMax = 170004,
219+
StickerFrameRateInvalid = 170006,
220+
StickerAnimationDurationExceedsMaximum = 170007,
221+
222+
CannotUpdateFinishedEvent = 180000,
223+
FailedToCreateStage = 180002,
224+
225+
MessageBlockedByAutomaticModeration = 200000,
226+
TitleBlockedByAutomaticModeration = 200001,
227+
ForumChannelWebhooksMustHaveThreadNameOrId = 220001,
228+
ForumChannelWebhooksCannotHaveBothThreadNameAndId = 220002,
229+
WebhooksCanOnlyCreateThreadsInForumChannels = 220003,
230+
WebhookServicesCannotBeUsedInForumChannels = 220004,
231+
MessageBlockedByHarmfulLinksFilter = 240000,
232+
233+
OnboardingRequirementsNotMet = 350000,
234+
BelowOnboardingRequirements = 350001,
235+
236+
FailedToBanUsers = 500000,
237+
PollVotingBlocked = 520000,
238+
PollExpired = 520001,
239+
InvalidChannelTypeForPollCreation = 520002,
240+
CannotEditPollMessage = 520003,
241+
CannotUseEmojiIncludedWithPoll = 520004,
242+
CannotExpireNonPollMessage = 520006,
243+
244+
_ => Unknown(u32),
245+
}
246+
}
247+
13248
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
14249
#[non_exhaustive]
15250
pub struct DiscordJsonError {
16251
/// The error code.
17-
pub code: i32,
252+
pub code: JsonErrorCode,
18253
/// The error message.
19254
pub message: FixedString,
20255
/// The full explained errors with their path in the request body.
@@ -56,7 +291,7 @@ impl ErrorResponse {
56291
status_code: r.status(),
57292
url: FixedString::from_str_trunc(r.url().as_str()),
58293
error: r.json().await.unwrap_or_else(|e| DiscordJsonError {
59-
code: -1,
294+
code: JsonErrorCode::Unknown(1),
60295
errors: FixedArray::empty(),
61296
message: format!("[Serenity] Could not decode json when receiving error response from discord:, {e}").trunc_into(),
62297
}),
@@ -252,7 +487,7 @@ mod test {
252487
#[tokio::test]
253488
async fn test_error_response_into() {
254489
let error = DiscordJsonError {
255-
code: 43121215,
490+
code: JsonErrorCode::Unknown(43121215),
256491
errors: FixedArray::empty(),
257492
message: FixedString::from_static_trunc("This is a Ferris error"),
258493
};

0 commit comments

Comments
 (0)