From 8599465d33b16b12da844f115fd10f9af4ad530f Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 17 Dec 2017 14:55:22 -0500 Subject: [PATCH] fs: migrate errors to internal/errors Throw ERR_INVALID_ARG_TYPE when validating arguments passed to WriteStream.prototype._write. Refs: https://github.com/nodejs/node/issues/17709 PR-URL: https://github.com/nodejs/node/pull/17719 Reviewed-By: Joyee Cheung Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/fs.js | 9 +++++++-- test/parallel/test-fs-write-stream.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index fe657c3c24f67d..94da3914c3fa3d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2554,8 +2554,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(); +}