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