How to handle unhandled rejections?

My app usually crashes when a very weird unhandled rejection occurs, usually when it's an event where I don't have a general error handler. So how can I catch unhandled rejections effectively? (Maybe even do something with it like send it with a Webhook or log it)
25 Replies
d.js toolkit
d.js toolkit•3mo 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!
treble/luna
treble/luna•3mo ago
Catch them properly, either by try catch or just chaining a .catch to your promises
LukeZ
LukeZOP•3mo ago
Oh well, nevermind 😂 I will give that one a try: https://discordjs.guide/popular-topics/errors.html#how-to-diagnose-api-errors
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
LukeZ
LukeZOP•3mo ago
Not really possible. I'd have to make a handler just for that which is a bit too complicated to implement
monbrey
monbrey•3mo ago
Yes, your errors should be handled Thats how that works
LukeZ
LukeZOP•3mo ago
Well, I'm dynamically loading my event listeners. I'd have to manually wrap the whole event function in a try {} catch {} Imma try this one first
treble/luna
treble/luna•3mo ago
You're gonna have to find a way, or live with your curremt issue
monbrey
monbrey•3mo ago
no you dont You can catch the error at the promise that throws it
LukeZ
LukeZOP•3mo ago
Oooh, I think I'm gonna do it like this (if the one from. The guide doesn't work properly)
// Handler Function
export default async handler(eventFunc: Function, ...args) {
try {
await eventFunc(...args);
} catch (e) { /* ... */ }
}

// Use the handler in the index
// In the loop where I import the event function
client.addEventListener(event, handlerFunc.bind(null, eventFunc, ...args));
// Handler Function
export default async handler(eventFunc: Function, ...args) {
try {
await eventFunc(...args);
} catch (e) { /* ... */ }
}

// Use the handler in the index
// In the loop where I import the event function
client.addEventListener(event, handlerFunc.bind(null, eventFunc, ...args));
Maybe like this idk I will see I know, but I sometimes have random rejections on promises that usually don't reject and if I'd handle all promises, my code would be flooded with try-catches. That seems not very practical to me and there has to be an easier and better solution.
monbrey
monbrey•3mo ago
nope
LukeZ
LukeZOP•3mo ago
What nope?
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
LukeZ
LukeZOP•3mo ago
So try-catch on every promise. :FeelsDonkMan: Btw. It's usually the functions themselves rejecting because of some edge case scenario I didn't think of
monbrey
monbrey•3mo ago
I mean, you can propagate errors upwards, but generally catching at the source will give you the best granularity and information to debug A higher level handler is fine to catch the ones youve missed
jacob
jacob•3mo ago
process
.on("unhandledRejection", (err: Error) => logger.error(inspect(err), "unhandledRejection"))
.on("uncaughtException", (err: Error) => logger.error(inspect(err), "uncaughtException"))
.on("error", (err: Error) => logger.error(inspect(err), "error"))
.on("warning", (err: Error) => logger.error(inspect(err), "warning"));
process
.on("unhandledRejection", (err: Error) => logger.error(inspect(err), "unhandledRejection"))
.on("uncaughtException", (err: Error) => logger.error(inspect(err), "uncaughtException"))
.on("error", (err: Error) => logger.error(inspect(err), "error"))
.on("warning", (err: Error) => logger.error(inspect(err), "warning"));
LukeZ
LukeZOP•3mo ago
👀
jacob
jacob•3mo ago
but it is generally easier to catch bugs when you .catch the promise instead of doing it on a higher level like the above code
LukeZ
LukeZOP•3mo ago
yeah I thought that too -# Not always possible though
jacob
jacob•3mo ago
its always possible
Amgelo
Amgelo•3mo ago
not a recommended practice
jacob
jacob•3mo ago
im aware, hence my warning above
Amgelo
Amgelo•3mo ago
yeah but it's not only easier, it's better
jacob
jacob•3mo ago
how is it easier to go through an entire codebase and add some code to the end of every promise than just using the code i sent there's pros and cons to it
Amgelo
Amgelo•3mo ago
I'm quoting you, just adding the fact that it's also better
jacob
jacob•3mo ago
ah apols high-level .catch: takes no time but less efficient low-level .catch: takes a long time but very efficient

Did you find this page helpful?