How to find which promises are not handled in nodejs UnhandledPromiseRejectionWarning?

nodejs from version 7 has asynchronous standby time for syntactic sugar to handle promises, the following warning quite often appears in my apis:

(node:11057) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: Error: Can't set headers after they are sent. (node:11057) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

Unfortunately, there is no link to a line that does not have a catch. Is there a way to find it without checking every catch try block?

+55
promise warnings async-await unhandled-exception
May 7 '17 at 17:31
source share
2 answers

listen to the unhandledRejection process event.

 process.on('unhandledRejection', (reason, p) => { console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); // application specific logging, throwing an error, or other logic here }); 
+107
May 16 '17 at 7:27
source share

The right way to show the full stack for ES6 Promise unhandled failures is to run Node.js with the --trace-warnings . This will show a complete snippet for each warning, without having to intercept the deviation inside your own code. For example:

 node --trace-warnings app.js

If you want to actually handle the raw rejects (for example, by registering them), you can use my unhandled-rejection module instead, which catches all the raw rejects for each main Promises implementation that supports it, with one event handler.

This module supports Bluebird, ES6 Promises, Q, WhenJS, es6-promise , then/promise and everything that matches any of the raw rejection specifications (full details in the documentation).

+1
Dec 19 '17 at 22:02
source share



All Articles