6

I'd like to trap when.js unhandled rejections so that I can log them. To accomplish this I've overriden console.warn(), however that can log stuff other than when.js which I'm not interested in.

ref: https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises

I am using prettymonitor with when.js https://github.com/AriaMinaei/pretty-monitor

nevf
  • 4,596
  • 6
  • 31
  • 32
  • By the way, this is the actual documentation is here https://github.com/cujojs/when/blob/master/docs/debug-api.md and the unhandled rejection tracking "spec" here: https://gist.github.com/benjamingr/0237932cee84712951a2 but the latter was written by a retard :( – Benjamin Gruenbaum Apr 17 '15 at 07:07

1 Answers1

4

If you're on the server-side, you can use the promise rejection hooks. These will work on most promise implementations on the server-side (io.js, bluebird, when, etc):

 process.on("unhandledRejection", function(promise, reason){
    // deal with the rejection here.
 });

If you're in a browser environment, things are less standardised. However, When still provides similar hooks there:

window.addEventListener('unhandledRejection', function(event) {
    event.preventDefault(); // This stops the initial log.
    // handle event
    event.detail.reason; // rejection reason
    event.detail.key; // rejection promise key
}, false);

There are also local rejection hooks, these are good if you only want to handle rejections of a single instance of the promise library - this is typically useful for when building a library yourself:

var Promise = require('when').Promise;
Promise.onPotentiallyUnhandledRejection = function(rejection) {
    // handle single instance error here
};
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Thanks so much. I couldn't get this to work at first as I was using when.js V2.51 in the Browser (V3.7 in Node). I've updated the Browser to V3.7 and window.addEventListener('unhandledRejection', ..) works a treat. I've also now found https://github.com/cujojs/when/blob/master/docs/debug-api.md which is a great help. – nevf Apr 17 '15 at 07:58
  • @nevf the global unhnandled rejection APIs for node/io.js are relatively new - as the author, I'd really appreciate feedback as you go - feel free to shoot me an email or post a response on the gist when/if you run into issues with the API exposed and/or have suggestions about it. – Benjamin Gruenbaum Apr 17 '15 at 08:05
  • I'll add this to my node.js app and if there are any issues I'll let you know. I've found the stack so ignore the text I just deleted. – nevf Apr 17 '15 at 10:03
  • the Node.js process.on() example prevents the normal console/prettymonitor output from displaying. Is there a way to retain this. Also the docs show: `process.on('unhandledRejection', function(reason, key)` not `..(promise,reason)`. ref: https://github.com/cujojs/when/blob/master/docs/debug-api.md – nevf Apr 18 '15 at 09:40
  • That's interesting, @Benjamin Gruenbaum. Because for me the solution does not work in the browser. This is probably because I use webpack and a shim for process. It seems initEmitRejection (using when.js 3.7.7) would skip emitting an event in this case. Can you comment on that? – Christian Oct 28 '16 at 14:14