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 toolkit2mo 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!
Kinect3000
Kinect30002mo ago
Prob just let the program die because the token is no longer valid Or ur talking abt forbidden
LukeZ
LukeZOP2mo ago
oh, my bad haha Yeah, forbidden and any additional errors
Kinect3000
Kinect30002mo ago
It depends on the context Like for interactions, reply ephemerally that you are missing permissions
LukeZ
LukeZOP2mo ago
When I try to timeout or ban someone for example
Kinect3000
Kinect30002mo ago
The status text usually gives you more insight Or the code in this case Error.code is not the status code btw
LukeZ
LukeZOP2mo ago
aaand how to access the code?
LukeZ
LukeZOP2mo ago
well, is it a DiscordjsError if it's an API error?
Kinect3000
Kinect30002mo ago
We should have an enum for api error codes
LukeZ
LukeZOP2mo ago
. is my approach at least correct?
Kinect3000
Kinect30002mo 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
LukeZOP2mo ago
🔍
No description
Kinect3000
Kinect30002mo ago
Huh, must be a generic Unauthorized then
LukeZ
LukeZOP2mo ago
is my approach at least correct?
Kinect3000
Kinect30002mo ago
The second if should be inside the first one
LukeZ
LukeZOP2mo ago
what :kekw:
Kinect3000
Kinect30002mo 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
LukeZOP2mo ago
error?.code or error instance of DiscordAPIError ?
Kinect3000
Kinect30002mo ago
The second if statement belongs in the first one
LukeZ
LukeZOP2mo ago
I'm not sure if I get it but you mean this? (Example)
No description
Kinect3000
Kinect30002mo ago
LukeZ
LukeZOP2mo ago
aaaaah noice -# I will mark this as solved as soon as it's working because I need to test it out tomorrow
Want results from more Discord servers?
Add your server