Skip to content

Commit 89dcf73

Browse files
BridgeARcodebytere
authored andcommitted
test: improve logged errors
To indicate which lines are test lines and which from Node.js core, it's good to rely on `util.inspect()` while inspecting errors. The stack was accessed directly instead in multiple cases and logging that does not provide as much information as using `util.inspect()`. PR-URL: #31425 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yihong Wang <[email protected]>
1 parent 1484f5a commit 89dcf73

14 files changed

+35
-30
lines changed

test/common/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,22 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
139139
process._rawDebug();
140140
throw new Error(`same id added to destroy list twice (${id})`);
141141
}
142-
destroyListList[id] = new Error().stack;
142+
destroyListList[id] = util.inspect(new Error());
143143
_queueDestroyAsyncId(id);
144144
};
145145

146146
require('async_hooks').createHook({
147-
init(id, ty, tr, r) {
147+
init(id, ty, tr, resource) {
148148
if (initHandles[id]) {
149149
process._rawDebug(
150-
`Is same resource: ${r === initHandles[id].resource}`);
150+
`Is same resource: ${resource === initHandles[id].resource}`);
151151
process._rawDebug(`Previous stack:\n${initHandles[id].stack}\n`);
152152
throw new Error(`init called twice for same id (${id})`);
153153
}
154-
initHandles[id] = { resource: r, stack: new Error().stack.substr(6) };
154+
initHandles[id] = {
155+
resource,
156+
stack: util.inspect(new Error()).substr(6)
157+
};
155158
},
156159
before() { },
157160
after() { },
@@ -161,7 +164,7 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
161164
process._rawDebug();
162165
throw new Error(`destroy called for same id (${id})`);
163166
}
164-
destroydIdsList[id] = new Error().stack;
167+
destroydIdsList[id] = util.inspect(new Error());
165168
},
166169
}).enable();
167170
}
@@ -345,7 +348,7 @@ function _mustCallInner(fn, criteria = 1, field) {
345348
const context = {
346349
[field]: criteria,
347350
actual: 0,
348-
stack: (new Error()).stack,
351+
stack: util.inspect(new Error()),
349352
name: fn.name || '<anonymous>'
350353
};
351354

@@ -530,7 +533,7 @@ function expectWarning(nameOrMap, expected, code) {
530533
function expectsError(validator, exact) {
531534
return mustCall((...args) => {
532535
if (args.length !== 1) {
533-
// Do not use `assert.strictEqual()` to prevent `util.inspect` from
536+
// Do not use `assert.strictEqual()` to prevent `inspect` from
534537
// always being called.
535538
assert.fail(`Expected one argument, got ${util.inspect(args)}`);
536539
}

test/common/wpt.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('fs');
77
const fsPromises = fs.promises;
88
const path = require('path');
99
const vm = require('vm');
10+
const { inspect } = require('util');
1011

1112
// https://github.com./w3c/testharness.js/blob/master/testharness.js
1213
// TODO: get rid of this half-baked harness in favor of the one
@@ -354,7 +355,7 @@ class WPTRunner {
354355
this.fail(filename, {
355356
name: '',
356357
message: err.message,
357-
stack: err.stack
358+
stack: inspect(err)
358359
}, 'UNCAUGHT');
359360
this.inProgress.delete(filename);
360361
}

test/fixtures/uncaught-exceptions/domain.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const domain = require('domain');
22

33
var d = domain.create();
44
d.on('error', function(err) {
5-
console.log('[ignored]', err.stack);
5+
console.log('[ignored]', err);
66
});
77

88
d.run(function() {

test/message/vm_display_runtime_error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ console.error('beginning');
2828
try {
2929
vm.runInThisContext('throw new Error("boo!")', { filename: 'test.vm' });
3030
} catch (err) {
31-
console.error(err.stack);
31+
console.error(err);
3232
}
3333

3434
vm.runInThisContext('throw new Error("spooky!")', { filename: 'test.vm' });

test/message/vm_display_syntax_error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ console.error('beginning');
2828
try {
2929
vm.runInThisContext('var 4;', { filename: 'foo.vm', displayErrors: true });
3030
} catch (err) {
31-
console.error(err.stack);
31+
console.error(err);
3232
}
3333

3434
vm.runInThisContext('var 5;', { filename: 'test.vm' });

test/parallel/test-assert-if-error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ assert.ifError(undefined);
8383
} catch (e) {
8484
threw = true;
8585
assert.strictEqual(e.message, 'Missing expected exception.');
86-
assert(!e.stack.includes('throws'), e.stack);
86+
assert(!e.stack.includes('throws'), e);
8787
}
8888
assert(threw);
8989
}

test/parallel/test-assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ assert.throws(
401401
threw = true;
402402
assert.ok(e.message.includes(rangeError.message));
403403
assert.ok(e instanceof assert.AssertionError);
404-
assert.ok(!e.stack.includes('doesNotThrow'), e.stack);
404+
assert.ok(!e.stack.includes('doesNotThrow'), e);
405405
}
406406
assert.ok(threw);
407407
}

test/parallel/test-http-request-agent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ server.listen(0, common.mustCall(function() {
3434
res.resume();
3535
server.close();
3636
})).on('error', function(e) {
37-
console.error(e.stack);
37+
console.error(e);
3838
process.exit(1);
3939
});
4040
}));

test/parallel/test-http-unix-socket.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ server.listen(common.PIPE, common.mustCall(function() {
6969
}));
7070

7171
req.on('error', function(e) {
72-
assert.fail(e.stack);
72+
assert.fail(e);
7373
});
7474

7575
req.end();

test/parallel/test-promises-unhandled-rejections.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const domain = require('domain');
5+
const { inspect } = require('util');
56

67
common.disableCrashOnUnhandledRejection();
78

@@ -14,8 +15,8 @@ const asyncTest = (function() {
1415

1516
function fail(error) {
1617
const stack = currentTest ?
17-
`${error.stack}\nFrom previous event:\n${currentTest.stack}` :
18-
error.stack;
18+
`${inspect(error)}\nFrom previous event:\n${currentTest.stack}` :
19+
inspect(error);
1920

2021
if (currentTest)
2122
process.stderr.write(`'${currentTest.description}' failed\n\n`);
@@ -44,11 +45,11 @@ const asyncTest = (function() {
4445
}
4546

4647
return function asyncTest(description, fn) {
47-
const stack = new Error().stack.split('\n').slice(1).join('\n');
48+
const stack = inspect(new Error()).split('\n').slice(1).join('\n');
4849
asyncTestQueue.push({
4950
action: fn,
50-
stack: stack,
51-
description: description
51+
stack,
52+
description
5253
});
5354
if (!asyncTestsEnabled) {
5455
asyncTestsEnabled = true;

test/parallel/test-tls-min-max-version.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const common = require('../common');
33
const fixtures = require('../common/fixtures');
4+
const { inspect } = require('util');
45

56
// Check min/max protocol versions.
67

@@ -16,7 +17,7 @@ function test(cmin, cmax, cprot, smin, smax, sprot, proto, cerr, serr) {
1617
// Report where test was called from. Strip leading garbage from
1718
// at Object.<anonymous> (file:line)
1819
// from the stack location, we only want the file:line part.
19-
const where = (new Error()).stack.split('\n')[2].replace(/[^(]*/, '');
20+
const where = inspect(new Error()).split('\n')[2].replace(/[^(]*/, '');
2021
connect({
2122
client: {
2223
checkServerIdentity: (servername, cert) => { },

test/parallel/test-tls-securepair-server.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const key = fixtures.readKey('rsa_private.pem');
3737
const cert = fixtures.readKey('rsa_cert.crt');
3838

3939
function log(a) {
40-
console.error(`***server*** ${a}`);
40+
console.error('***server***', a);
4141
}
4242

4343
const server = net.createServer(common.mustCall(function(socket) {
@@ -74,21 +74,18 @@ const server = net.createServer(common.mustCall(function(socket) {
7474
pair.cleartext.on('error', function(err) {
7575
log('got error: ');
7676
log(err);
77-
log(err.stack);
7877
socket.destroy();
7978
});
8079

8180
pair.encrypted.on('error', function(err) {
8281
log('encrypted error: ');
8382
log(err);
84-
log(err.stack);
8583
socket.destroy();
8684
});
8785

8886
socket.on('error', function(err) {
8987
log('socket error: ');
9088
log(err);
91-
log(err.stack);
9289
socket.destroy();
9390
});
9491

@@ -99,7 +96,6 @@ const server = net.createServer(common.mustCall(function(socket) {
9996
pair.on('error', function(err) {
10097
log('secure error: ');
10198
log(err);
102-
log(err.stack);
10399
socket.destroy();
104100
});
105101
}));

test/parallel/test-tls-set-ciphers.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22
const common = require('../common');
3-
if (!common.hasCrypto) common.skip('missing crypto');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
46
const fixtures = require('../common/fixtures');
7+
const { inspect } = require('util');
58

69
// Test cipher: option for TLS.
710

@@ -12,7 +15,7 @@ const {
1215

1316
function test(cciphers, sciphers, cipher, cerr, serr) {
1417
assert(cipher || cerr || serr, 'test missing any expectations');
15-
const where = (new Error()).stack.split('\n')[2].replace(/[^(]*/, '');
18+
const where = inspect(new Error()).split('\n')[2].replace(/[^(]*/, '');
1619
connect({
1720
client: {
1821
checkServerIdentity: (servername, cert) => { },

test/parallel/test-vm-context.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'use strict';
2323
require('../common');
2424
const assert = require('assert');
25-
25+
const { inspect } = require('util');
2626
const vm = require('vm');
2727
const Script = vm.Script;
2828
let script = new Script('"passed";');
@@ -59,7 +59,7 @@ try {
5959
} catch (e) {
6060
gh1140Exception = e;
6161
assert.ok(/expected-filename/.test(e.stack),
62-
`expected appearance of filename in Error stack: ${e.stack}`);
62+
`expected appearance of filename in Error stack: ${inspect(e)}`);
6363
}
6464
// This is outside of catch block to confirm catch block ran.
6565
assert.strictEqual(gh1140Exception.toString(), 'Error');

0 commit comments

Comments
 (0)