I'm using an event handler to catch unhandled promise rejections, found on SO:
process.on('unhandledRejection', function(error, promise) {
console.error('UNHANDLED REJECTION - Promise: ', promise, ', Error: ', error, ').');
});
Since I'm using nodejs + express, it's tricky to print out line numbers for code statements. I have a customized console.log2 method for doing so, but I'm trying to acplish the same with the rejection handler.
I've tried various caller/stack trace tricks with error stacks and such, but none of those methods correctly show what line the reject() statement is on.
EDIT
To clarify, the print out I get keeps showing the line number that the console.error statement above is on (thus same line everytime), then works backwards through the various promise library files/lines. It pletely skips/misses the line that the reject() statement itself is on.
I'm using an event handler to catch unhandled promise rejections, found on SO:
process.on('unhandledRejection', function(error, promise) {
console.error('UNHANDLED REJECTION - Promise: ', promise, ', Error: ', error, ').');
});
Since I'm using nodejs + express, it's tricky to print out line numbers for code statements. I have a customized console.log2 method for doing so, but I'm trying to acplish the same with the rejection handler.
I've tried various caller/stack trace tricks with error stacks and such, but none of those methods correctly show what line the reject() statement is on.
EDIT
To clarify, the print out I get keeps showing the line number that the console.error statement above is on (thus same line everytime), then works backwards through the various promise library files/lines. It pletely skips/misses the line that the reject() statement itself is on.
Share Improve this question edited Nov 9, 2017 at 22:49 Karric asked Nov 9, 2017 at 22:33 KarricKarric 1,5752 gold badges22 silver badges34 bronze badges 5- @JaromandaX - yes, .trace did not work either. Will clarify in question. – Karric Commented Nov 9, 2017 at 22:46
-
Did you just try code inspection to find where you're using
.then()
without a.catch()
? Might find more than just the one causing this event. – jfriend00 Commented Nov 10, 2017 at 3:53 - @jfriend00 - It's not about a specific rejection instance, I just want a generalized handler and I'd like it to include printing the caller line number. – Karric Commented Nov 10, 2017 at 14:48
- I do not think the built-in promise library currently provides that capability. With a proper configuration and the Bluebird promise library, you can get details that work like a stack trace for Bluebird promise rejections. – jfriend00 Commented Nov 10, 2017 at 15:18
- @jfriend00 - I'm trying to avoid dependencies if I can. It's not super essential, I was just hoping it could be done. – Karric Commented Nov 11, 2017 at 13:40
1 Answer
Reset to default 7depends very much on the libraries, I couldn't reproduce it, but I suggest you to try with this alternative:
process.on('unhandledRejection', up => { throw up });
Should be more effective than just reporting promise and error, but maybe in your specific situation it may also not work properly. Just, give it a try!...