diff --git a/lib/fs.js b/lib/fs.js index 9554620a040a3a..7d25db7280236a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2379,8 +2379,13 @@ WriteStream.prototype.open = function() { WriteStream.prototype._write = function(data, encoding, cb) { - if (!(data instanceof Buffer)) - return this.emit('error', new Error('Invalid data')); + if (!(data instanceof Buffer)) { + const err = new errors.TypeError('ERR_INVALID_ARG_TYPE', + 'data', + 'Buffer', + data); + return this.emit('error', err); + } if (typeof this.fd !== 'number') { return this.once('open', function() { diff --git a/test/parallel/test-fs-write-stream.js b/test/parallel/test-fs-write-stream.js index bba2debf9156e5..1dc25547a9b5b0 100644 --- a/test/parallel/test-fs-write-stream.js +++ b/test/parallel/test-fs-write-stream.js @@ -49,3 +49,16 @@ common.refreshTmpDir(); }); stream.destroy(); } + +// Throws if data is not of type Buffer. +{ + const stream = fs.createWriteStream(file); + common.expectsError(() => { + stream._write(42, null, function() {}); + }, { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "data" argument must be of type Buffer. Received type number' + }); + stream.destroy(); +}