-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add Promise
API (Bonus: finally support)
#47
Comments
Hi, Do I understand correctly that For example, I try to use it on an
Do you know of a way to globally catch any async errors, even those occuring inside promises? I try to not use the global Thanks in advance! |
It's actually totally possible, but a little more invasive than try catch
already is. Effectively, you would need to track all Errors in
Error.prepareStackTrace, and then filter for unhandledRejection errors.
You could implement this fairly easily by slightly augmenting an existing
userspace implementation of onUnhandledRejection, but instead of emitting
the error on the process.onUnhandledRejection event, you would just pass it
to the current context's active catch call back / handler (when it exists)
instead.
This wouldn't really add any additional functionality that is not already
provided via the process.onUnhandledRejection event and long stack traces,
except the ability to catch in context. It would be like a request (500)
level unhandledRejection.
That is something that would be nice to have.
|
First thanks for your reply! I'm quite new to Node.js so I have to admit I don't understand everything you say, sadly.
How can I do that? Something like tweaking the Or are you talking about writing code inside the global handler (we use
Because, in this handler, I'm not sure how I could pass anything "to the current context's active catch call back / handler (when it exists)". How can I make it return a Are you able to provide some quick code snippets to get me started? This is the first time I have to implement error management in an async framework, and I find it quite difficult. Thanks again! |
This would be implemented in Off the top of my head, the way It's not immediately obvious how to accomplish the above without mucking with / coupling the implementation to node.js internals (e.g.,
Given the above, merely adding the following should/might be sufficient: // process.domain.emit cannot be passed directly b/c it changes dynamically
process.on('unhandledRejection', function(e) {
// This will be caught be the active domain...
// ... And avoids emitting on other unhandledRejection handlers
// Active domain error handler is the relevant trycatch catch function
if (process.domain && !process.domain._disposed) throw e
})
// Run the above just before the hookit call that wrap core...
// ... to avoid wrapping the handler (and mucking with the active domain)
// ... Though given the special EventEmitter logic for process, this probably doesn't matter
// Pass callback wrapping function to hookit
hookit(wrap) You could verify this by adding a test that does the following: var err = new Error('This should get passed to catchFn')
trycatch(function catchFn() {
setImmediate(function() {
// Create a rejected promise that has no reject handler
Promise.reject(err)
})
}, function catchFn(e) {
assert.equals(err, e)
}) The existing test suite (
Finally, this will only work for promise implementations that properly emit Let me know if that works. Thanks for taking the time to work on this. Happy to help as best I can given my limited availability. |
I have always used this middleware in
The default error handler is able in this way to find what happened and provide a nice colorized long stack trace with the detail of the error. This has saved me a lot of time in debugging. Lately promises with no
But I would still like to see What can I do to help? |
BTW, after a few attempts this works with the problem that I currently have:
It also seems to work on native promises. Just make sure you add:
Before:
However, the long stack trace is not completed! |
An opt-in
Promise
API is the best avenue for adding finally support.Likely, the callback API will be deprecated at a point that makes sense and avoids as much inconvenience as possible for the community
The text was updated successfully, but these errors were encountered: