Skip to content

Commit

Permalink
stream: do not throw multiple callback errors in writable
Browse files Browse the repository at this point in the history
Align ERR_MULTIPLE_CALLBACK in Writable with the rest
of error handling as well as how the error is implemented
in Transform.

PR-URL: #30614
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ronag authored and Trott committed Dec 14, 2019
1 parent decc5f5 commit e902fad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,10 @@ function onwrite(stream, er) {
const sync = state.sync;
const cb = state.writecb;

if (typeof cb !== 'function')
throw new ERR_MULTIPLE_CALLBACK();
if (typeof cb !== 'function') {
errorOrDestroy(stream, new ERR_MULTIPLE_CALLBACK());
return;
}

state.writing = false;
state.writecb = null;
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-stream-writable-callback-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const { Writable } = require('stream');
const stream = new Writable({
write(chunk, enc, cb) { cb(); cb(); }
});

stream.on('error', common.expectsError({
type: Error,
message: 'Callback called multiple times',
code: 'ERR_MULTIPLE_CALLBACK'
}));

stream.write('foo');

0 comments on commit e902fad

Please sign in to comment.