diff --git a/lib/net.js b/lib/net.js index 1533843dbab981..278153d3bdffc7 100644 --- a/lib/net.js +++ b/lib/net.js @@ -42,6 +42,7 @@ const WriteWrap = process.binding('stream_wrap').WriteWrap; const async_id_symbol = process.binding('async_wrap').async_id_symbol; const { newUid, setInitTriggerId } = require('async_hooks'); const nextTick = require('internal/process/next_tick').nextTick; +const errors = require('internal/errors'); var cluster; var dns; @@ -964,8 +965,9 @@ Socket.prototype.connect = function() { this._sockname = null; } - var pipe = !!options.path; - debug('pipe', pipe, options.path); + const path = options.path; + var pipe = !!path; + debug('pipe', pipe, path); if (!this._handle) { this._handle = pipe ? new Pipe() : new TCP(); @@ -982,7 +984,13 @@ Socket.prototype.connect = function() { this.writable = true; if (pipe) { - internalConnect(this, options.path); + if (typeof path !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', + 'options.path', + 'string', + path); + } + internalConnect(this, path); } else { lookupAndConnect(this, options); } diff --git a/test/parallel/test-net-better-error-messages-path.js b/test/parallel/test-net-better-error-messages-path.js index f4d00c7aebf055..bb2256637a0d30 100644 --- a/test/parallel/test-net-better-error-messages-path.js +++ b/test/parallel/test-net-better-error-messages-path.js @@ -2,12 +2,21 @@ const common = require('../common'); const net = require('net'); const assert = require('assert'); -const fp = '/tmp/fadagagsdfgsdf'; -const c = net.connect(fp); -c.on('connect', common.mustNotCall()); +{ + const fp = '/tmp/fadagagsdfgsdf'; + const c = net.connect(fp); -c.on('error', common.mustCall(function(e) { - assert.strictEqual(e.code, 'ENOENT'); - assert.strictEqual(e.message, `connect ENOENT ${fp}`); -})); + c.on('connect', common.mustNotCall()); + c.on('error', common.expectsError({ + code: 'ENOENT', + message: `connect ENOENT ${fp}` + })); +} + +{ + assert.throws( + () => net.createConnection({ path: {} }), + common.expectsError({ code: 'ERR_INVALID_ARG_TYPE' }) + ); +}