From 0ecdf2934039b6e847aa3d1441e3ec235a70d125 Mon Sep 17 00:00:00 2001 From: mskec Date: Mon, 13 Feb 2017 17:28:51 +0100 Subject: [PATCH] errors: migrate lib/console Migrate console.js to use internal/errors.js. PR-URL: https://github.com/nodejs/node/pull/11340 Ref: https://github.com/nodejs/node/issues/11273 Reviewed-By: James M Snell Reviewed-By: Franziska Hinkelmann --- doc/api/errors.md | 7 +++++++ lib/console.js | 5 +++-- lib/internal/errors.js | 2 ++ test/parallel/test-console-instance.js | 28 ++++++++++++++++++-------- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 40b8e7ab74cf71..6b20e81b487347 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -570,6 +570,13 @@ The `'ERR_ARG_NOT_ITERABLE'` error code is used generically to identify that an iterable argument (i.e. a value that works with `for...of` loops) is required, but not provided to a Node.js API. + +### ERR_CONSOLE_WRITABLE_STREAM + +The `ERR_CONSOLE_WRITABLE_STREAM` error code is thrown when `Console` is +instantiated without `stdout` stream or when `stdout` or `stderr` streams +are not writable. + ### ERR_INVALID_ARG_TYPE diff --git a/lib/console.js b/lib/console.js index 7ec9c846329cce..642ef209d6b975 100644 --- a/lib/console.js +++ b/lib/console.js @@ -21,6 +21,7 @@ 'use strict'; +const errors = require('internal/errors'); const util = require('util'); function Console(stdout, stderr, ignoreErrors = true) { @@ -28,12 +29,12 @@ function Console(stdout, stderr, ignoreErrors = true) { return new Console(stdout, stderr, ignoreErrors); } if (!stdout || typeof stdout.write !== 'function') { - throw new TypeError('Console expects a writable stream instance'); + throw new errors.TypeError('ERR_CONSOLE_WRITABLE_STREAM', 'stdout'); } if (!stderr) { stderr = stdout; } else if (typeof stderr.write !== 'function') { - throw new TypeError('Console expects writable stream instances'); + throw new errors.TypeError('ERR_CONSOLE_WRITABLE_STREAM', 'stderr'); } var prop = { diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 1ca0f59d793b41..885b4e3af28391 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -112,6 +112,8 @@ module.exports = exports = { // Note: Please try to keep these in alphabetical order E('ERR_ARG_NOT_ITERABLE', '%s must be iterable'); E('ERR_ASSERTION', (msg) => msg); +E('ERR_CONSOLE_WRITABLE_STREAM', + (name) => `Console expects a writable stream instance for ${name}`); E('ERR_INVALID_ARG_TYPE', invalidArgType); E('ERR_INVALID_CALLBACK', 'callback must be a function'); E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s'); diff --git a/test/parallel/test-console-instance.js b/test/parallel/test-console-instance.js index dee0cae5430c22..1e68de3f6ebbfa 100644 --- a/test/parallel/test-console-instance.js +++ b/test/parallel/test-console-instance.js @@ -37,16 +37,28 @@ assert.strictEqual('function', typeof Console); // make sure that the Console constructor throws // when not given a writable stream instance -assert.throws(() => { - new Console(); -}, /^TypeError: Console expects a writable stream instance$/); +assert.throws( + () => { new Console(); }, + common.expectsError({ + code: 'ERR_CONSOLE_WRITABLE_STREAM', + type: TypeError, + message: /stdout/ + }) +); // Console constructor should throw if stderr exists but is not writable -assert.throws(() => { - out.write = common.noop; - err.write = undefined; - new Console(out, err); -}, /^TypeError: Console expects writable stream instances$/); +assert.throws( + () => { + out.write = common.noop; + err.write = undefined; + new Console(out, err); + }, + common.expectsError({ + code: 'ERR_CONSOLE_WRITABLE_STREAM', + type: TypeError, + message: /stderr/ + }) +); out.write = err.write = (d) => {};