Skip to content

Commit

Permalink
stream: forward errored to callback
Browse files Browse the repository at this point in the history
Refs: #39356
  • Loading branch information
ronag committed Jul 12, 2021
1 parent de85b1e commit 2f9bc11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,12 @@ function errorBuffer(state) {
const { chunk, callback } = state.buffered[n];
const len = state.objectMode ? 1 : chunk.length;
state.length -= len;
callback(new ERR_STREAM_DESTROYED('write'));
callback(state.errored ?? new ERR_STREAM_DESTROYED('write'));
}

const onfinishCallbacks = state[kOnFinished].splice(0);
for (let i = 0; i < onfinishCallbacks.length; i++) {
onfinishCallbacks[i](new ERR_STREAM_DESTROYED('end'));
onfinishCallbacks[i](state.errored ?? new ERR_STREAM_DESTROYED('end'));
}

resetBuffer(state);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,16 @@ const assert = require('assert');
write.destroy();
write.destroy();
}

{
// https://github.com/nodejs/node/issues/39356
const s = new Writable({
final() {}
});
const _err = new Error('oh no');
// Remove `callback` and it works
s.end(common.mustCall((err) => {
assert.strictEqual(err, _err);
}));
s.destroy(_err);
}
5 changes: 3 additions & 2 deletions test/parallel/test-stream-writable-end-cb-uncaught.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ process.on('uncaughtException', common.mustCall((err) => {
}));

const writable = new stream.Writable();
const _err = new Error('kaboom');

writable._write = (chunk, encoding, cb) => {
cb();
};
writable._final = (cb) => {
cb(new Error('kaboom'));
cb(_err);
};

writable.write('asd');
writable.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
assert.strictEqual(err, _err);
}));

0 comments on commit 2f9bc11

Please sign in to comment.