nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.93k stars 29.76k forks source link

domain does not add properties when error occurs in Promise context #46468

Open Sebmaster opened 1 year ago

Sebmaster commented 1 year ago

Version

v18.13.0

Platform

Darwin KFQ4RYQV5M 22.3.0 Darwin Kernel Version 22.3.0: Thu Jan 5 20:48:54 PST 2023; root:xnu-8792.81.2~2/RELEASE_ARM64_T6000 arm64

Subsystem

domain

What steps will reproduce the bug?

const assert = require('node:assert');
const domain = require('node:domain');

const d = domain.create();
d.on('error', (e) => {
  assert.ok(e.domain); // throws
  console.log('Success');
});

d.run(() => {
  new Promise((resolve) => {
    throw new Error('oops');
  });
});

vs

const assert = require('node:assert');
const domain = require('node:domain');

const d = domain.create();
d.on('error', (e) => {
  assert.ok(e.domain); // all good
  console.log('Yay');
});

d.run(() => {
  throw new Error('oops');
});

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

The docs mention:

Any time an Error object is routed through a domain, a few extra fields are added to it.

So I'd expect error.domain to be defined when looped through the domain.

What do you see instead?

When erroring within a Promise context, domain does not add the expected properties.

Additional information

Noticed this one when migrating off of Bluebird to native Promises.

bnoordhuis commented 1 year ago

That line in the documentation predates the introduction of promises. There's another section in that same document that says this:

Domains will not interfere with the error handling mechanisms for promises.

I'd say the documentation needs an update. I'll add the label, PR welcome.