diff --git a/lib/net.js b/lib/net.js index dc96526683..09c582d9d7 100644 --- a/lib/net.js +++ b/lib/net.js @@ -619,8 +619,10 @@ Socket.prototype.__defineGetter__('localPort', function() { Socket.prototype.write = function(chunk, encoding, cb) { - if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) - throw new TypeError('Invalid data'); + if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { + throw new TypeError( + 'Invalid data, chunk must be a string or buffer, not ' + typeof chunk); + } return stream.Duplex.prototype.write.apply(this, arguments); }; diff --git a/test/parallel/test-net-socket-write-error.js b/test/parallel/test-net-socket-write-error.js new file mode 100644 index 0000000000..db236be1a5 --- /dev/null +++ b/test/parallel/test-net-socket-write-error.js @@ -0,0 +1,18 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +const server = net.createServer().listen(common.PORT, connectToServer); + +function connectToServer() { + const client = net.createConnection(common.PORT, () => { + assert.throws(() => { + client.write(1337); + }, /Invalid data, chunk must be a string or buffer, not number/); + + client.end(); + }) + .on('end', () => server.close()); +}