Node: Unexpected "UnhandledPromiseRejectionWarning" instead of "unhandled 'error' event"

Created on 23 Oct 2016  路  4Comments  路  Source: nodejs/node

const { EventEmitter }  = require('events')

const emitter = new EventEmitter()

const errFn = () => new Promise((resolve, reject) => {
  reject(new Error('woot'))
})

errFn().catch((err) => {
  emitter.emit('error', err)
})

Curiously, the above code produces the following output:

(node:75098) UnhandledPromiseRejectionWarning: Unhandled promiserejection (rejection id: 2): Error: woot

The promise rejection is handled, what isn't handled is the error event that is subsequently emitted. Expected output should be:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: woot
Node v6.6.0
promises

Most helpful comment

Ah, I didn't realize that the error was actually thrown synchronously by emitter.emit('error', err) and was catchable. So I guess it's not really a bug then? The annoying thing though is that there's no way to know that the error is because of an Unhandled 'error' event.

Right, that is the case at the moment. You can add an "error" event handler to the emitter which will take care of it.

Alternatively, you can run node with --trace-warnings which will tell you where the issue is.

As there is no action item at this point I'm going to close this, but feel free to follow up with a a comment, issue or a PR about how we can communicate these things better.

All 4 comments

This happens because the Promise completely swallows throws that occur even within the catch() handler. Those end up as unhandled promise rejections internally. Since the built in Promise implementation in V8 does not give us appropriate visibility to see what's happening inside, we cannot trigger the unhandled error event.

Ah, I didn't realize that the error was actually thrown synchronously by emitter.emit('error', err) and was catchable. So I guess it's not really a bug then? The annoying thing though is that there's no way to know that the error is because of an Unhandled 'error' event.

You'd have to add another .catch to catch the emitter error.

Ah, I didn't realize that the error was actually thrown synchronously by emitter.emit('error', err) and was catchable. So I guess it's not really a bug then? The annoying thing though is that there's no way to know that the error is because of an Unhandled 'error' event.

Right, that is the case at the moment. You can add an "error" event handler to the emitter which will take care of it.

Alternatively, you can run node with --trace-warnings which will tell you where the issue is.

As there is no action item at this point I'm going to close this, but feel free to follow up with a a comment, issue or a PR about how we can communicate these things better.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Brekmister picture Brekmister  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

loretoparisi picture loretoparisi  路  3Comments

Icemic picture Icemic  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments