sindresorhus / loud-rejection

Make unhandled promise rejections fail loudly instead of the default silent fail

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I was going to clean it up, I promise!

LinusU opened this issue · comments

I don't know if there is much we can do about this, but it would be very cool if this didn't throw an error.

var loudRejection = require('loud-rejection')

loudRejection()

var myp

setTimeout(function () {
  myp = new Promise(function (resolve, reject) {
    setTimeout(reject, 100, new Error('Silence me'))
  })
}, 100)

setTimeout(function () {
  myp.catch(function (err) {
    console.log('Catched!')
  })
}, 300)

I think that it has to go into the Promise implementation thought, and I guess that that ship has sailed.

My idea was to do this when the promise is thrown away by the garbage collector, if it hasn't been handled by then it won't ever be. But maybe this has problems as well?

Hmm, since this is for CLI tools I could defer the console.error and exiting until process.on('exit').

See the docs: https://nodejs.org/api/process.html#process_event_unhandledrejection

Could probably do something like the example in https://nodejs.org/api/process.html#process_event_rejectionhandled and keep track of unhandled rejection that are not eventually caught.

Hmm, yes that could possible work actually. Nice find!

Pseudo code:

var unhandledRejections = [];
process.on('unhandledRejection', function(reason, p) {
    unhandledRejections.push(p);
});
process.on('rejectionHandled', function(p) {
    var index = unhandledRejections.indexOf(p);
    unhandledRejections.splice(index, 1);
});

process.on('exit', function () {
  if (unhandledRejections.length > 0) {
    unhandledRejections.forEach(function (x) {
      console.error(x.reason);
    });
    process.exitCode = 1;
  }
});

Wanna do a PR?

Sure! Might take a day thought 🚀

@LinusU Any progress? No worries if not. Just asking as I'd like to include this in AVA, but it's blocked by this issue.

None yet, I'm on the airport going home now so if I'm not too tired I'll fix it tonight...

Closed via #5