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 <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
This commit is contained in:
parent
ae5e65c72f
commit
0ecdf29340
@ -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,
|
iterable argument (i.e. a value that works with `for...of` loops) is required,
|
||||||
but not provided to a Node.js API.
|
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>
|
<a id="ERR_INVALID_ARG_TYPE"></a>
|
||||||
### ERR_INVALID_ARG_TYPE
|
### ERR_INVALID_ARG_TYPE
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const errors = require('internal/errors');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
|
||||||
function Console(stdout, stderr, ignoreErrors = true) {
|
function Console(stdout, stderr, ignoreErrors = true) {
|
||||||
@ -28,12 +29,12 @@ function Console(stdout, stderr, ignoreErrors = true) {
|
|||||||
return new Console(stdout, stderr, ignoreErrors);
|
return new Console(stdout, stderr, ignoreErrors);
|
||||||
}
|
}
|
||||||
if (!stdout || typeof stdout.write !== 'function') {
|
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) {
|
if (!stderr) {
|
||||||
stderr = stdout;
|
stderr = stdout;
|
||||||
} else if (typeof stderr.write !== 'function') {
|
} 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 = {
|
var prop = {
|
||||||
|
@ -112,6 +112,8 @@ module.exports = exports = {
|
|||||||
// Note: Please try to keep these in alphabetical order
|
// Note: Please try to keep these in alphabetical order
|
||||||
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
|
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
|
||||||
E('ERR_ASSERTION', (msg) => msg);
|
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_ARG_TYPE', invalidArgType);
|
||||||
E('ERR_INVALID_CALLBACK', 'callback must be a function');
|
E('ERR_INVALID_CALLBACK', 'callback must be a function');
|
||||||
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');
|
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');
|
||||||
|
@ -37,16 +37,28 @@ assert.strictEqual('function', typeof Console);
|
|||||||
|
|
||||||
// make sure that the Console constructor throws
|
// make sure that the Console constructor throws
|
||||||
// when not given a writable stream instance
|
// when not given a writable stream instance
|
||||||
assert.throws(() => {
|
assert.throws(
|
||||||
new Console();
|
() => { new Console(); },
|
||||||
}, /^TypeError: Console expects a writable stream instance$/);
|
common.expectsError({
|
||||||
|
code: 'ERR_CONSOLE_WRITABLE_STREAM',
|
||||||
|
type: TypeError,
|
||||||
|
message: /stdout/
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
// Console constructor should throw if stderr exists but is not writable
|
// Console constructor should throw if stderr exists but is not writable
|
||||||
assert.throws(() => {
|
assert.throws(
|
||||||
out.write = common.noop;
|
() => {
|
||||||
err.write = undefined;
|
out.write = common.noop;
|
||||||
new Console(out, err);
|
err.write = undefined;
|
||||||
}, /^TypeError: Console expects writable stream instances$/);
|
new Console(out, err);
|
||||||
|
},
|
||||||
|
common.expectsError({
|
||||||
|
code: 'ERR_CONSOLE_WRITABLE_STREAM',
|
||||||
|
type: TypeError,
|
||||||
|
message: /stderr/
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
out.write = err.write = (d) => {};
|
out.write = err.write = (d) => {};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user