From b13d561e708e84280ccb98e00cf8e77ff708a8d0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 17 Feb 2017 18:15:09 +0100 Subject: [PATCH] console: fixup `console.dir()` error handling Apply the `console: do not emit error events` changes properly to `console.dir()`. This was overlooked in f18e08d820dde161788d95a5603546ceca021e90 (https://github.com/nodejs/node/pull/9744). Ref: https://github.com/nodejs/node/commit/f18e08d820dde161788d95a5603546ceca021e90#commitcomment-20934407 --- lib/console.js | 5 +- .../test-console-async-write-error.js | 20 +++-- .../parallel/test-console-sync-write-error.js | 82 ++++++++++--------- 3 files changed, 57 insertions(+), 50 deletions(-) diff --git a/lib/console.js b/lib/console.js index 7c4e4bb786e180..6de9d43ef3359c 100644 --- a/lib/console.js +++ b/lib/console.js @@ -103,7 +103,10 @@ Console.prototype.error = Console.prototype.warn; Console.prototype.dir = function dir(object, options) { options = Object.assign({customInspect: false}, options); - write(this._ignoreErrors, this._stdout, `${util.inspect(object, options)}\n`); + write(this._ignoreErrors, + this._stdout, + `${util.inspect(object, options)}\n`, + this._stdoutErrorHandler); }; diff --git a/test/parallel/test-console-async-write-error.js b/test/parallel/test-console-async-write-error.js index 0fcd72868ab090..63e8bfbc881e4c 100644 --- a/test/parallel/test-console-async-write-error.js +++ b/test/parallel/test-console-async-write-error.js @@ -4,14 +4,16 @@ const { Console } = require('console'); const { Writable } = require('stream'); const assert = require('assert'); -const out = new Writable({ - write: common.mustCall((chunk, enc, callback) => { - process.nextTick(callback, new Error('foobar')); - }) -}); +for (const method of ['dir', 'log', 'warn']) { + const out = new Writable({ + write: common.mustCall((chunk, enc, callback) => { + process.nextTick(callback, new Error('foobar')); + }) + }); -const c = new Console(out, out, true); + const c = new Console(out, out, true); -assert.doesNotThrow(() => { - c.log('abc'); -}); + assert.doesNotThrow(() => { + c[method]('abc'); + }); +} diff --git a/test/parallel/test-console-sync-write-error.js b/test/parallel/test-console-sync-write-error.js index 34ff8bad8c9f3d..fb350d463bb35d 100644 --- a/test/parallel/test-console-sync-write-error.js +++ b/test/parallel/test-console-sync-write-error.js @@ -4,44 +4,46 @@ const { Console } = require('console'); const { Writable } = require('stream'); const assert = require('assert'); -{ - const out = new Writable({ - write: common.mustCall((chunk, enc, callback) => { - callback(new Error('foobar')); - }) - }); - - const c = new Console(out, out, true); - - assert.doesNotThrow(() => { - c.log('abc'); - }); -} - -{ - const out = new Writable({ - write: common.mustCall((chunk, enc, callback) => { - throw new Error('foobar'); - }) - }); - - const c = new Console(out, out, true); - - assert.doesNotThrow(() => { - c.log('abc'); - }); -} - -{ - const out = new Writable({ - write: common.mustCall((chunk, enc, callback) => { - setImmediate(() => callback(new Error('foobar'))); - }) - }); - - const c = new Console(out, out, true); - - assert.doesNotThrow(() => { - c.log('abc'); - }); +for (const method of ['dir', 'log', 'warn']) { + { + const out = new Writable({ + write: common.mustCall((chunk, enc, callback) => { + callback(new Error('foobar')); + }) + }); + + const c = new Console(out, out, true); + + assert.doesNotThrow(() => { + c[method]('abc'); + }); + } + + { + const out = new Writable({ + write: common.mustCall((chunk, enc, callback) => { + throw new Error('foobar'); + }) + }); + + const c = new Console(out, out, true); + + assert.doesNotThrow(() => { + c[method]('abc'); + }); + } + + { + const out = new Writable({ + write: common.mustCall((chunk, enc, callback) => { + setImmediate(() => callback(new Error('foobar'))); + }) + }); + + const c = new Console(out, out, true); + + assert.doesNotThrow(() => { + c[method]('abc'); + }); + } }