From 39f4158bc38ff2925b86563082c8d26ab3d20b17 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 18 Mar 2019 15:41:19 +0100 Subject: lib: move extra properties into error creation This encapsulates the Node.js errors more by adding extra properties to an error inside of the function to create the error message instead of adding the properties at the call site. That simplifies the usage of our errors and makes sure the expected properties are always set. PR-URL: https://github.com/nodejs/node/pull/26752 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/net.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/net.js') diff --git a/lib/net.js b/lib/net.js index ecf7258e8b..a5f012ac33 100644 --- a/lib/net.js +++ b/lib/net.js @@ -968,19 +968,20 @@ function lookupAndConnect(self, options) { if (!self.connecting) return; if (err) { - // net.createConnection() creates a net.Socket object and - // immediately calls net.Socket.connect() on it (that's us). - // There are no event listeners registered yet so defer the - // error event to the next tick. + // net.createConnection() creates a net.Socket object and immediately + // calls net.Socket.connect() on it (that's us). There are no event + // listeners registered yet so defer the error event to the next tick. + // TODO(BridgeAR): The error could either originate from user code or + // by the C++ layer. The port is never the cause for the error as it is + // not used in the lookup. We should probably just remove this. err.host = options.host; err.port = options.port; err.message = err.message + ' ' + options.host + ':' + options.port; process.nextTick(connectErrorNT, self, err); } else if (addressType !== 4 && addressType !== 6) { - err = new ERR_INVALID_ADDRESS_FAMILY(addressType); - err.host = options.host; - err.port = options.port; - err.message = err.message + ' ' + options.host + ':' + options.port; + err = new ERR_INVALID_ADDRESS_FAMILY(addressType, + options.host, + options.port); process.nextTick(connectErrorNT, self, err); } else { self._unrefTimer(); -- cgit v1.2.3