console: check that stderr is writable
`Console` constructor checks that `stdout.write()` is a function but does not do an equivalent check for `stderr.write()`. If `stderr` is not specified in the constructor, then `stderr` is set to be `stdout`. However, if `stderr` is specified, but `stderr.write()` is not a function, then an exception is not thrown until `console.error()` is called. This change adds the same check for 'stderr' in the constructor that is there for `stdout`. If `stderr` fails the check, then a `TypeError` is thrown. Took the opportunity to copyedit the `console` doc a little too. PR-URL: https://github.com/nodejs/node/pull/5635 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rod Vagg <rod@vagg.org>
This commit is contained in:
parent
2a0b3daa4c
commit
6ba5af2c2c
@ -48,8 +48,8 @@ myConsole.warn(`Danger ${name}! Danger!`);
|
|||||||
```
|
```
|
||||||
|
|
||||||
While the API for the `Console` class is designed fundamentally around the
|
While the API for the `Console` class is designed fundamentally around the
|
||||||
Web browser `console` object, the `Console` in Node.js is *not* intended to
|
browser `console` object, the `Console` in Node.js is *not* intended to
|
||||||
duplicate the browsers functionality exactly.
|
duplicate the browser's functionality exactly.
|
||||||
|
|
||||||
## Asynchronous vs Synchronous Consoles
|
## Asynchronous vs Synchronous Consoles
|
||||||
|
|
||||||
@ -75,8 +75,8 @@ const Console = console.Console;
|
|||||||
|
|
||||||
Creates a new `Console` by passing one or two writable stream instances.
|
Creates a new `Console` by passing one or two writable stream instances.
|
||||||
`stdout` is a writable stream to print log or info output. `stderr`
|
`stdout` is a writable stream to print log or info output. `stderr`
|
||||||
is used for warning or error output. If `stderr` isn't passed, the warning
|
is used for warning or error output. If `stderr` isn't passed, warning and error
|
||||||
and error output will be sent to the `stdout`.
|
output will be sent to `stdout`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const output = fs.createWriteStream('./stdout.log');
|
const output = fs.createWriteStream('./stdout.log');
|
||||||
|
@ -11,7 +11,10 @@ function Console(stdout, stderr) {
|
|||||||
}
|
}
|
||||||
if (!stderr) {
|
if (!stderr) {
|
||||||
stderr = stdout;
|
stderr = stdout;
|
||||||
|
} else if (typeof stderr.write !== 'function') {
|
||||||
|
throw new TypeError('Console expects writable stream instances');
|
||||||
}
|
}
|
||||||
|
|
||||||
var prop = {
|
var prop = {
|
||||||
writable: true,
|
writable: true,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
require('../common');
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var Stream = require('stream');
|
const Stream = require('stream');
|
||||||
var Console = require('console').Console;
|
const Console = require('console').Console;
|
||||||
var called = false;
|
var called = false;
|
||||||
|
|
||||||
|
const out = new Stream();
|
||||||
|
const err = new Stream();
|
||||||
|
|
||||||
// ensure the Console instance doesn't write to the
|
// ensure the Console instance doesn't write to the
|
||||||
// process' "stdout" or "stderr" streams
|
// process' "stdout" or "stderr" streams
|
||||||
process.stdout.write = process.stderr.write = function() {
|
process.stdout.write = process.stderr.write = function() {
|
||||||
@ -20,9 +23,13 @@ assert.throws(function() {
|
|||||||
new Console();
|
new Console();
|
||||||
}, /Console expects a writable stream/);
|
}, /Console expects a writable stream/);
|
||||||
|
|
||||||
var out = new Stream();
|
// Console constructor should throw if stderr exists but is not writable
|
||||||
var err = new Stream();
|
assert.throws(function() {
|
||||||
out.writable = err.writable = true;
|
out.write = function() {};
|
||||||
|
err.write = undefined;
|
||||||
|
new Console(out, err);
|
||||||
|
}, /Console expects writable stream instances/);
|
||||||
|
|
||||||
out.write = err.write = function(d) {};
|
out.write = err.write = function(d) {};
|
||||||
|
|
||||||
var c = new Console(out, err);
|
var c = new Console(out, err);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user