Skip to content

Commit e48ec70

Browse files
addaleaxcodebytere
authored andcommitted
domain: improve deprecation warning text for DEP0097
Because the following gives basically no actionable information on its own, neither in the error message nor in the stack trace: (node:3187) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead. at emitMakeCallbackDeprecation (domain.js:123:13) at process.topLevelDomainCallback (domain.js:135:5) at process.callbackTrampoline (internal/async_hooks.js:124:14) PR-URL: #36136 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent d8fcf2c commit e48ec70

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎lib/domain.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,23 @@ process.setUncaughtExceptionCaptureCallback = function(fn) {
124124

125125

126126
letsendMakeCallbackDeprecation=false;
127-
functionemitMakeCallbackDeprecation(){
127+
functionemitMakeCallbackDeprecation({ target, method }){
128128
if(!sendMakeCallbackDeprecation){
129129
process.emitWarning(
130130
'Using a domain property in MakeCallback is deprecated. Use the '+
131131
'async_context variant of MakeCallback or the AsyncResource class '+
132-
'instead.','DeprecationWarning','DEP0097');
132+
'instead. '+
133+
`(Triggered by calling ${method?.name??'<anonymous>'} `+
134+
`on ${target?.constructor?.name}.)`,
135+
'DeprecationWarning','DEP0097');
133136
sendMakeCallbackDeprecation=true;
134137
}
135138
}
136139

137140
functiontopLevelDomainCallback(cb, ...args){
138141
constdomain=this.domain;
139142
if(exports.active&&domain)
140-
emitMakeCallbackDeprecation();
143+
emitMakeCallbackDeprecation({target: this,method: cb});
141144

142145
if(domain)
143146
domain.enter();

‎test/addons/make-callback-domain-warning/test.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const domain = require('domain');
66
constbinding=require(`./build/${common.buildType}/binding`);
77

88
functionmakeCallback(object,cb){
9-
binding.makeCallback(object,()=>setImmediate(cb));
9+
binding.makeCallback(object,functionsomeMethod(){setImmediate(cb);});
1010
}
1111

1212
letlatestWarning=null;
@@ -16,8 +16,14 @@ process.on('warning', (warning) => {
1616

1717
constd=domain.create();
1818

19+
classResource{
20+
constructor(domain){
21+
this.domain=domain;
22+
}
23+
}
24+
1925
// When domain is disabled, no warning will be emitted
20-
makeCallback({domain: d},common.mustCall(()=>{
26+
makeCallback(newResource(d),common.mustCall(()=>{
2127
assert.strictEqual(latestWarning,null);
2228

2329
d.run(common.mustCall(()=>{
@@ -26,7 +32,9 @@ makeCallback({ domain: d }, common.mustCall(() => {
2632
assert.strictEqual(latestWarning,null);
2733

2834
// Warning is emitted when domain property is used and domain is enabled
29-
makeCallback({domain: d},common.mustCall(()=>{
35+
makeCallback(newResource(d),common.mustCall(()=>{
36+
assert.match(latestWarning.message,
37+
/TriggeredbycallingsomeMethodonResource\./);
3038
assert.strictEqual(latestWarning.name,'DeprecationWarning');
3139
assert.strictEqual(latestWarning.code,'DEP0097');
3240
}));

0 commit comments

Comments
 (0)