Skip to content

Node and IO silently exits when error thrown in promise catch #600

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
webuniverseio opened this issue Jan 25, 2015 · 3 comments
Closed

Node and IO silently exits when error thrown in promise catch #600

webuniverseio opened this issue Jan 25, 2015 · 3 comments

Comments

@webuniverseio
Copy link

Run this code in iojs

setTimeout(function () {
    throw new Error('Weee');
}, 1000);

You'll get an error and a minimal stack trace in console.

throw new Error('Weee');
          ^
Error: Weee
    at null._onTimeout (path to error)
    at Timer.listOnTimeout (timers.js:109:15)

Process finished with exit code 1

Run following code in iojs

new Promise(function (f, r) {
    throw new Error('Weee');
}).catch(function (ex) {
    throw new Error('Fuuu');
});

You'll get nothing, even though catch function definitely executes (you can try adding console.log in there)

Process finished with exit code 0

I'm not sure sure how it is supposed to work, but looks suspicious.

@webuniverseio webuniverseio changed the title Node silently exits when error thrown in promise catch Node and IO silently exits when error thrown in promise catch Jan 25, 2015
@vkurchatkin
Copy link
Contributor

Thanks for the report!

In fact all throws inside promise handlers are caught by promises themselves:

new Promise(function (f, r) {
  throw new Error('Weee');
}).catch(function (ex) {
  throw new Error('Fuuu');
}).catch(function(){
  console.log('hey');
});

The problem is that currently there is no mechanism to deal with unhandled promise rejections, so they are silently ignored. This issue is tracked here: #256

Closing as a duplicate.

@webuniverseio
Copy link
Author

Oh, I thought it was handled if I provide catch, but it will create yet another promise, so makes sense. I looked at how Chrome is working with my example and it seems that there is no difference from developer stand point if code executes like above, or like that:

new Promise(function (f, r) {
    throw new Error('Weee');
}).catch(function (ex) {
    setTimeout(function () {throw new Error('Fuuu');}, 0);
});

Later is working as expected in node & io, so I'll use that pattern from now on.

@benjamingr
Copy link
Member

@szarouski note that in promise libraries like bluebird this is not a silent failure but is reported with a stack trace to the console.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants