Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: migrate lib/console #11340

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a id="ERR_CONSOLE_WRITABLE_STREAM"></a>
### 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.

<a id="ERR_INVALID_ARG_TYPE"></a>
### ERR_INVALID_ARG_TYPE

Expand Down
5 changes: 3 additions & 2 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@

'use strict';

const errors = require('internal/errors');
const util = require('util');

function Console(stdout, stderr, ignoreErrors = true) {
if (!(this instanceof Console)) {
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 = {
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the %s syntax already used by several other format strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No special reason. I made this PR when there was no other error messages using %s syntax.
Will change if you think that way is better

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');
Expand Down
28 changes: 20 additions & 8 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {};

Expand Down