diff --git a/lib/internal/fs/sync_write_stream.js b/lib/internal/fs/sync_write_stream.js index 5fff43f43a76c9..f8fbade88393b4 100644 --- a/lib/internal/fs/sync_write_stream.js +++ b/lib/internal/fs/sync_write_stream.js @@ -23,7 +23,12 @@ ObjectSetPrototypeOf(SyncWriteStream.prototype, Writable.prototype); ObjectSetPrototypeOf(SyncWriteStream, Writable); SyncWriteStream.prototype._write = function(chunk, encoding, cb) { - writeSync(this.fd, chunk, 0, chunk.length); + try { + writeSync(this.fd, chunk, 0, chunk.length); + } catch (e) { + cb(e); + return; + } cb(); }; diff --git a/test/parallel/test-internal-fs-syncwritestream.js b/test/parallel/test-internal-fs-syncwritestream.js index b93d3f01d875ee..e0d0289cbcef85 100644 --- a/test/parallel/test-internal-fs-syncwritestream.js +++ b/test/parallel/test-internal-fs-syncwritestream.js @@ -79,3 +79,15 @@ const filename = path.join(tmpdir.path, 'sync-write-stream.txt'); assert.strictEqual(stream.fd, null); })); } + +// Verify that an error on _write() triggers an 'error' event. +{ + const fd = fs.openSync(filename, 'w'); + const stream = new SyncWriteStream(fd); + + assert.strictEqual(stream.fd, fd); + stream._write({}, null, common.mustCall((err) => { + assert(err); + fs.closeSync(fd); + })); +}