Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: improve socket.write() error message #5981

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-net-socket-write-error.js
Original file line number Diff line number Diff line change
@@ -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());
}