Skip to content

Commit 78beb44

Browse files
committed
console: do not emit error events
1 parent 52fd49b commit 78beb44

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

lib/_stream_writable.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function WritableState(options, stream) {
8787

8888
// the callback that's passed to _write(chunk,cb)
8989
this.onwrite = function(er) {
90-
onwrite(stream, er);
90+
return onwrite(stream, er);
9191
};
9292

9393
// the callback that the user supplies to write(chunk,encoding,cb)
@@ -337,10 +337,14 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
337337

338338
function onwriteError(stream, state, sync, er, cb) {
339339
--state.pendingcb;
340-
if (sync)
340+
341+
if (sync) {
341342
process.nextTick(cb, er);
342-
else
343-
cb(er);
343+
} else {
344+
if (cb(er) === doNotEmitErrorEvent) {
345+
return doNotEmitErrorEvent;
346+
}
347+
}
344348

345349
stream._writableState.errorEmitted = true;
346350
stream.emit('error', er);
@@ -361,7 +365,7 @@ function onwrite(stream, er) {
361365
onwriteStateUpdate(state);
362366

363367
if (er)
364-
onwriteError(stream, state, sync, er, cb);
368+
return onwriteError(stream, state, sync, er, cb);
365369
else {
366370
// Check if we're actually ready to finish, but don't emit yet
367371
var finished = needFinish(state);
@@ -555,3 +559,7 @@ function CorkedRequest(state) {
555559
}
556560
};
557561
}
562+
563+
564+
const doNotEmitErrorEvent = Symbol('doNotEmitErrorEvent');
565+
Writable.doNotEmitErrorEvent = doNotEmitErrorEvent;

lib/console.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
const util = require('util');
4+
const { doNotEmitErrorEvent } = require('stream').Writable;
45

5-
function Console(stdout, stderr) {
6+
function Console(stdout, stderr, ignoreErrors) {
67
if (!(this instanceof Console)) {
7-
return new Console(stdout, stderr);
8+
return new Console(stdout, stderr, ignoreErrors);
89
}
910
if (!stdout || typeof stdout.write !== 'function') {
1011
throw new TypeError('Console expects a writable stream instance');
@@ -24,6 +25,8 @@ function Console(stdout, stderr) {
2425
Object.defineProperty(this, '_stdout', prop);
2526
prop.value = stderr;
2627
Object.defineProperty(this, '_stderr', prop);
28+
prop.value = ignoreErrors === undefined ? true : !!ignoreErrors;
29+
Object.defineProperty(this, '_ignoreErrors', prop);
2730
prop.value = new Map();
2831
Object.defineProperty(this, '_times', prop);
2932

@@ -35,20 +38,32 @@ function Console(stdout, stderr) {
3538
}
3639
}
3740

41+
function write(ignoreErrors, stream, string) {
42+
if (!ignoreErrors) return stream.write(string);
43+
44+
try {
45+
stream.write(string, (err) => {
46+
return doNotEmitErrorEvent;
47+
});
48+
} catch (e) {
49+
// Sorry, there’s proper way to pass along the error here.
50+
}
51+
}
52+
3853

3954
// As of v8 5.0.71.32, the combination of rest param, template string
4055
// and .apply(null, args) benchmarks consistently faster than using
4156
// the spread operator when calling util.format.
4257
Console.prototype.log = function log(...args) {
43-
this._stdout.write(`${util.format.apply(null, args)}\n`);
58+
write(this._ignoreErrors, this._stdout, `${util.format.apply(null, args)}\n`);
4459
};
4560

4661

4762
Console.prototype.info = Console.prototype.log;
4863

4964

5065
Console.prototype.warn = function warn(...args) {
51-
this._stderr.write(`${util.format.apply(null, args)}\n`);
66+
write(this._ignoreErrors, this._stderr, `${util.format.apply(null, args)}\n`);
5267
};
5368

5469

@@ -57,7 +72,7 @@ Console.prototype.error = Console.prototype.warn;
5772

5873
Console.prototype.dir = function dir(object, options) {
5974
options = Object.assign({customInspect: false}, options);
60-
this._stdout.write(`${util.inspect(object, options)}\n`);
75+
write(this._ignoreErrors, this._stdout, `${util.inspect(object, options)}\n`);
6176
};
6277

6378

lib/net.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,15 @@ Socket.prototype.destroySoon = function() {
463463
this.once('finish', this.destroy);
464464
};
465465

466-
467466
Socket.prototype._destroy = function(exception, cb) {
468467
debug('destroy');
469468

470469
function fireErrorCallbacks(self) {
471-
if (cb) cb(exception);
470+
if (cb) {
471+
if (cb(exception) === stream.Writable.doNotEmitErrorEvent)
472+
return;
473+
}
474+
472475
if (exception && !self._writableState.errorEmitted) {
473476
process.nextTick(emitErrorNT, self, exception);
474477
self._writableState.errorEmitted = true;

0 commit comments

Comments
 (0)