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