How to find which promises are unhandled in Node.js UnhandledPromiseRejectionWarning?

node.jsPromiseAsync AwaitWarningsUnhandled Exception

node.js Problem Overview


Node.js from version 7 has async/await syntactic sugar for handling promises and now in my code the following warning comes up quite often:

(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's no reference to the line where the catch is missing. Is there any way to find it without checking every try/catch block?

node.js Solutions


Solution 1 - node.js

listen unhandledRejection event of process.

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});

Solution 2 - node.js

The correct way to show a full stacktrace for unhandled ES6 Promise rejections, is to run Node.js with the --trace-warnings flag. This will show the full stacktrace for every warning, without having to intercept the rejection from within your own code. For example:

node --trace-warnings app.js

Ensure that the trace-warnings flag comes before the name of your .js file! Otherwise, the flag will be interpreted as an argument to your script, and it will be ignored by Node.js itself.

If you want to actually handle unhandled rejections (eg. by logging them), then you might want to use my unhandled-rejection module instead, which catches all the unhandled rejections for every major Promises implementation that supports it, with a single event handler.

That module supports Bluebird, ES6 Promises, Q, WhenJS, es6-promise, then/promise, and anything that conforms to any of the unhandled rejection specifications (full details in the documentation).

Solution 3 - node.js

Logging with stack trace

If you are looking for more of a helpful error message. Try adding this to your node file. It should display the full stack trace where your crash is happening.

process.on('unhandledRejection', (error, p) => {
  console.log('=== UNHANDLED REJECTION ===');
  console.dir(error.stack);
});

Solution 4 - node.js

This module allowed me to track down the culprit promise(s): https://www.npmjs.com/package/trace-unhandled

  1. Install

    npm i trace-unhandled
    
  2. Include in code

    require('trace-unhandled/register');
    

Solution 5 - node.js

@Jeremy I had same result, the reason variable provided by

process.on('unhandledRejection', (reason, p) => {});

wasn't defined and it take me time to figure out that there were promise rejection providing nothing in my code, typically:

new Promise((resolve reject) => {
  const client = net.connect(options)
  client.on("connect", () => {
    resolve()
  })
  client.on("error", () => {
    reject()
  })
})

the problem is that you reject with nothing, to get trace you have to provide error, like this

new Promise((resolve reject) => {
  const client = net.connect(options)
  client.on("connect", () => {
    resolve()
  })
  client.on("error", (err) => {
    reject(err)
  })
})

and if you don't have error you can provide one yourself, even if empty, it will give a stack trace

reject(new Error())

If you need to find where the error was throwed from, look in your code for Promise with empty rejection

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1658162View Question on Stackoverflow
Solution 1 - node.jscuixipingView Answer on Stackoverflow
Solution 2 - node.jsSven SlootwegView Answer on Stackoverflow
Solution 3 - node.jsjoshuakcockrellView Answer on Stackoverflow
Solution 4 - node.jsegekhterView Answer on Stackoverflow
Solution 5 - node.jsDevTheJoView Answer on Stackoverflow