Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you get unhandled promise?

If an error condition arises inside a promise, you “reject” the promise by calling the reject() function with an error. To handle a promise rejection, you pass a callback to the catch() function. This is a simple example, so catching the rejection is trivial.

How do you terminate the node process in unhandled promise rejection?

catch(). To terminate the node process on unhandled promise rejection, use the CLI flag '--unhandled-rejections=strict' (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:89219) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.

What is an unhandled promise rejection?

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()

How does async await handle rejection?

Find out how to reject a Promise in async/await syntax Therefore, you simply need to throw an error inside the async function to make it reject. For example: function wait(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } async function foo() { await wait(1000); throw new Error('Woops! '); } console.


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
});

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).


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);
});

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');