How to handle HTTP errors?

https://discordjs.guide/popular-topics/errors I've read this and still don't know how to properly handle HTTP errors. I don't really know how to handle a 403 (FORBIDDEN) error for example. How can I do this? Update: Can I just do this?
import { DiscordAPIError, RESTJSONErrorCodes } from "discord.js";

try {
} catch (err) {
// Handle HTTP errors
if (err instanceof DiscordAPIError) {
// Handle here
}

// JSON error codes
if (err.code === RESTJSONErrorCodes.Unauthorized) {
// Handle here
}
}
import { DiscordAPIError, RESTJSONErrorCodes } from "discord.js";

try {
} catch (err) {
// Handle HTTP errors
if (err instanceof DiscordAPIError) {
// Handle here
}

// JSON error codes
if (err.code === RESTJSONErrorCodes.Unauthorized) {
// Handle here
}
}
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
23 Replies
d.js toolkit
d.js toolkit5mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Inky
Inky5mo ago
Prob just let the program die because the token is no longer valid Or ur talking abt forbidden
LukeZ
LukeZOP5mo ago
oh, my bad haha Yeah, forbidden and any additional errors
Inky
Inky5mo ago
It depends on the context Like for interactions, reply ephemerally that you are missing permissions
LukeZ
LukeZOP5mo ago
When I try to timeout or ban someone for example
Inky
Inky5mo ago
The status text usually gives you more insight Or the code in this case Error.code is not the status code btw
LukeZ
LukeZOP5mo ago
aaand how to access the code?
LukeZ
LukeZOP5mo ago
well, is it a DiscordjsError if it's an API error?
Inky
Inky5mo ago
We should have an enum for api error codes
LukeZ
LukeZOP5mo ago
. is my approach at least correct?
Inky
Inky5mo ago
discord-api-types documentation
discord-api-types - Imagine typings
discord-api-types is a simple Node/Deno module that brings up to date typings for Discord's API
LukeZ
LukeZOP5mo ago
🔍
No description
Inky
Inky5mo ago
Huh, must be a generic Unauthorized then
LukeZ
LukeZOP5mo ago
is my approach at least correct?
Inky
Inky5mo ago
The second if should be inside the first one
LukeZ
LukeZOP5mo ago
what :kekw:
Inky
Inky5mo ago
You can only guarantee error.code exists if error is a DiscordAPIError Otherwise, error could be undefined or null for all you know
LukeZ
LukeZOP5mo ago
error?.code or error instance of DiscordAPIError ?
Inky
Inky5mo ago
The second if statement belongs in the first one
LukeZ
LukeZOP5mo ago
I'm not sure if I get it but you mean this? (Example)
No description
Inky
Inky5mo ago
LukeZ
LukeZOP5mo ago
aaaaah noice -# I will mark this as solved as soon as it's working because I need to test it out tomorrow

Did you find this page helpful?