-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detecting unhandled rejections #87
Comments
Thanks for this issue and for the interesting approach. Interestingly, i've started looking into this myself a few days ago :) As you said, the only way to not swallow exceptions is to end every promise chain with There are some issues that need to be resolved first, but i definitely will look into this then. |
@jsor Is there any update on this matter? |
This would help so much for people new to Promises / ReactPHP. There are many ways to hurt yourself... I have found a lot of them :-). Making it also work for EventEmitters would be a big help too: finding all emitters that do not have an In the docs it says to use |
FWIW, I'm using arnaud-lb@90e6e35 in production with this tracer for years now, and it has successfully detected any unhandled rejection since then. Feel free to make a PR from the diff :) |
Thank you. I had a go at making something based on your gist. It is here https://github.com/pavarnos/promise/tree/tracer. I hit a few snags I could not resolve by myself
Some examples
|
Just opened a PR to implement this: #170 |
When using promises, there are a good chance that some code will forget to handle rejections/exceptions in a promise chain. This has the result of effectively hidding any rejection/exception that occurred during the execution of a promise's callback, which is dangerous and can lead to undetected bugs.
What makes this worse is that is suffices of a single error/forget in a promise chain to hide all exceptions thrown anywhere in the chain, including errors in react-php: reactphp/http-client#31, reactphp/dns#26.
It's super easy to not handle a rejection: if a promise chain is ended by anything except
done()
, any errors in the chain will be unhandled and potentially hidden:This example is quite obvious: any exception thrown in doSomething() is caught and hidden from the user.
Other example from react/http-client:
This one is less obvious. In this example, any exception thrown in retry() is caught by the promise and effectively hidden from the user.
Some promise implementations are trying to detect unhandled rejections, and log them: http://bluebirdjs.com/docs/api/error-management-configuration.html
It is possible to do so in react/promise too. For that, we would have to simply watch when promises are instantiated, handled, and destructed. If a non-handled promise is destructed, it means that an unhandled rejection occurred.
Here is a PoC adding tracing functionality to promises: arnaud-lb@90e6e35
And here is a PoC tracer that detects unhandled rejections: https://gist.github.com/arnaud-lb/a2a5a5480bbd80013f756ff968282936
This can be used like this:
This immediately discovered a few bugs in my code. I'm using this since a few months already, and this prevents the introduction of new bugs.
The text was updated successfully, but these errors were encountered: