Skip to content

Commit c969047

Browse files
committed
console: fixup console.dir() error handling
Apply the `console: do not emit error events` changes properly to `console.dir()`. This was overlooked in f18e08d (#9744). Ref: f18e08d#commitcomment-20934407 PR-URL: #11443 Reviewed-By: James M Snell <[email protected]>
1 parent 1934686 commit c969047

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

lib/console.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ Console.prototype.error = Console.prototype.warn;
103103

104104
Console.prototype.dir = function dir(object, options) {
105105
options = Object.assign({customInspect: false}, options);
106-
write(this._ignoreErrors, this._stdout, `${util.inspect(object, options)}\n`);
106+
write(this._ignoreErrors,
107+
this._stdout,
108+
`${util.inspect(object, options)}\n`,
109+
this._stdoutErrorHandler);
107110
};
108111

109112

test/parallel/test-console-async-write-error.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ const { Console } = require('console');
44
const { Writable } = require('stream');
55
const assert = require('assert');
66

7-
const out = new Writable({
8-
write: common.mustCall((chunk, enc, callback) => {
9-
process.nextTick(callback, new Error('foobar'));
10-
})
11-
});
7+
for (const method of ['dir', 'log', 'warn']) {
8+
const out = new Writable({
9+
write: common.mustCall((chunk, enc, callback) => {
10+
process.nextTick(callback, new Error('foobar'));
11+
})
12+
});
1213

13-
const c = new Console(out, out, true);
14+
const c = new Console(out, out, true);
1415

15-
assert.doesNotThrow(() => {
16-
c.log('abc');
17-
});
16+
assert.doesNotThrow(() => {
17+
c[method]('abc');
18+
});
19+
}

test/parallel/test-console-sync-write-error.js

+42-40
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,46 @@ const { Console } = require('console');
44
const { Writable } = require('stream');
55
const assert = require('assert');
66

7-
{
8-
const out = new Writable({
9-
write: common.mustCall((chunk, enc, callback) => {
10-
callback(new Error('foobar'));
11-
})
12-
});
13-
14-
const c = new Console(out, out, true);
15-
16-
assert.doesNotThrow(() => {
17-
c.log('abc');
18-
});
19-
}
20-
21-
{
22-
const out = new Writable({
23-
write: common.mustCall((chunk, enc, callback) => {
24-
throw new Error('foobar');
25-
})
26-
});
27-
28-
const c = new Console(out, out, true);
29-
30-
assert.doesNotThrow(() => {
31-
c.log('abc');
32-
});
33-
}
34-
35-
{
36-
const out = new Writable({
37-
write: common.mustCall((chunk, enc, callback) => {
38-
setImmediate(() => callback(new Error('foobar')));
39-
})
40-
});
41-
42-
const c = new Console(out, out, true);
43-
44-
assert.doesNotThrow(() => {
45-
c.log('abc');
46-
});
7+
for (const method of ['dir', 'log', 'warn']) {
8+
{
9+
const out = new Writable({
10+
write: common.mustCall((chunk, enc, callback) => {
11+
callback(new Error('foobar'));
12+
})
13+
});
14+
15+
const c = new Console(out, out, true);
16+
17+
assert.doesNotThrow(() => {
18+
c[method]('abc');
19+
});
20+
}
21+
22+
{
23+
const out = new Writable({
24+
write: common.mustCall((chunk, enc, callback) => {
25+
throw new Error('foobar');
26+
})
27+
});
28+
29+
const c = new Console(out, out, true);
30+
31+
assert.doesNotThrow(() => {
32+
c[method]('abc');
33+
});
34+
}
35+
36+
{
37+
const out = new Writable({
38+
write: common.mustCall((chunk, enc, callback) => {
39+
setImmediate(() => callback(new Error('foobar')));
40+
})
41+
});
42+
43+
const c = new Console(out, out, true);
44+
45+
assert.doesNotThrow(() => {
46+
c[method]('abc');
47+
});
48+
}
4749
}

0 commit comments

Comments
 (0)