Skip to content

Commit 0ecdf29

Browse files
mskecfhinkel
authored andcommitted
errors: migrate lib/console
Migrate console.js to use internal/errors.js. PR-URL: #11340 Ref: #11273 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent ae5e65c commit 0ecdf29

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,13 @@ The `'ERR_ARG_NOT_ITERABLE'` error code is used generically to identify that an
570570
iterable argument (i.e. a value that works with `for...of` loops) is required,
571571
but not provided to a Node.js API.
572572

573+
<a id="ERR_CONSOLE_WRITABLE_STREAM"></a>
574+
### ERR_CONSOLE_WRITABLE_STREAM
575+
576+
The `ERR_CONSOLE_WRITABLE_STREAM` error code is thrown when `Console` is
577+
instantiated without `stdout` stream or when `stdout` or `stderr` streams
578+
are not writable.
579+
573580
<a id="ERR_INVALID_ARG_TYPE"></a>
574581
### ERR_INVALID_ARG_TYPE
575582

lib/console.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@
2121

2222
'use strict';
2323

24+
const errors = require('internal/errors');
2425
const util = require('util');
2526

2627
function Console(stdout, stderr, ignoreErrors = true) {
2728
if (!(this instanceof Console)) {
2829
return new Console(stdout, stderr, ignoreErrors);
2930
}
3031
if (!stdout || typeof stdout.write !== 'function') {
31-
throw new TypeError('Console expects a writable stream instance');
32+
throw new errors.TypeError('ERR_CONSOLE_WRITABLE_STREAM', 'stdout');
3233
}
3334
if (!stderr) {
3435
stderr = stdout;
3536
} else if (typeof stderr.write !== 'function') {
36-
throw new TypeError('Console expects writable stream instances');
37+
throw new errors.TypeError('ERR_CONSOLE_WRITABLE_STREAM', 'stderr');
3738
}
3839

3940
var prop = {

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ module.exports = exports = {
112112
// Note: Please try to keep these in alphabetical order
113113
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
114114
E('ERR_ASSERTION', (msg) => msg);
115+
E('ERR_CONSOLE_WRITABLE_STREAM',
116+
(name) => `Console expects a writable stream instance for ${name}`);
115117
E('ERR_INVALID_ARG_TYPE', invalidArgType);
116118
E('ERR_INVALID_CALLBACK', 'callback must be a function');
117119
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');

test/parallel/test-console-instance.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,28 @@ assert.strictEqual('function', typeof Console);
3737

3838
// make sure that the Console constructor throws
3939
// when not given a writable stream instance
40-
assert.throws(() => {
41-
new Console();
42-
}, /^TypeError: Console expects a writable stream instance$/);
40+
assert.throws(
41+
() => { new Console(); },
42+
common.expectsError({
43+
code: 'ERR_CONSOLE_WRITABLE_STREAM',
44+
type: TypeError,
45+
message: /stdout/
46+
})
47+
);
4348

4449
// Console constructor should throw if stderr exists but is not writable
45-
assert.throws(() => {
46-
out.write = common.noop;
47-
err.write = undefined;
48-
new Console(out, err);
49-
}, /^TypeError: Console expects writable stream instances$/);
50+
assert.throws(
51+
() => {
52+
out.write = common.noop;
53+
err.write = undefined;
54+
new Console(out, err);
55+
},
56+
common.expectsError({
57+
code: 'ERR_CONSOLE_WRITABLE_STREAM',
58+
type: TypeError,
59+
message: /stderr/
60+
})
61+
);
5062

5163
out.write = err.write = (d) => {};
5264

0 commit comments

Comments
 (0)