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
- 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!Catch them properly, either by try catch or just chaining a .catch to your promises
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.
Not really possible. I'd have to make a handler just for that which is a bit too complicated to implement
Yes, your errors should be handled
Thats how that works
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 firstYou're gonna have to find a way, or live with your curremt issue
no you dont
You can catch the error at the promise that throws it
Oooh, I think I'm gonna do it like this (if the one from. The guide doesn't work properly)
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.
nope
What nope?
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
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
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
👀
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
yeah I thought that too
-# Not always possible though
its always possible
not a recommended practice
im aware, hence my warning above
yeah but it's not only easier, it's better
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
I'm quoting you, just adding the fact that it's also better
ah apols
high-level .catch: takes no time but less efficient
low-level .catch: takes a long time but very efficient