-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
lib/net: Convert to using internal/errors #14782
Changes from 9 commits
0cc1ea6
00b8f49
16d8519
90eb1a5
056dfcc
7373ee2
787682b
74d6b0b
5a4e982
30a1ca0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,10 +61,10 @@ const normalizedArgsSymbol = internalNet.normalizedArgsSymbol; | |
function noop() {} | ||
|
||
function createHandle(fd) { | ||
var type = TTYWrap.guessHandleType(fd); | ||
const type = TTYWrap.guessHandleType(fd); | ||
if (type === 'PIPE') return new Pipe(); | ||
if (type === 'TCP') return new TCP(); | ||
throw new TypeError('Unsupported fd type: ' + type); | ||
throw new errors.TypeError('ERR_INVALID_FD_TYPE', type); | ||
} | ||
|
||
|
||
|
@@ -695,8 +695,10 @@ protoGetter('localPort', function localPort() { | |
|
||
Socket.prototype.write = function(chunk, encoding, cb) { | ||
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { | ||
throw new TypeError( | ||
'Invalid data, chunk must be a string or buffer, not ' + typeof chunk); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | ||
'chunk', | ||
['string', 'Buffer'], | ||
chunk); | ||
} | ||
return stream.Duplex.prototype.write.apply(this, arguments); | ||
}; | ||
|
@@ -1035,21 +1037,25 @@ function lookupAndConnect(self, options) { | |
var localPort = options.localPort; | ||
|
||
if (localAddress && !cares.isIP(localAddress)) { | ||
throw new TypeError('"localAddress" option must be a valid IP: ' + | ||
localAddress); | ||
throw new errors.TypeError('ERR_INVALID_IP_ADDRESS', localAddress); | ||
} | ||
|
||
if (localPort && typeof localPort !== 'number') { | ||
throw new TypeError('"localPort" option should be a number: ' + localPort); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | ||
'options.localPort', | ||
'number', | ||
localPort); | ||
} | ||
|
||
if (typeof port !== 'undefined') { | ||
if (typeof port !== 'number' && typeof port !== 'string') { | ||
throw new TypeError('"port" option should be a number or string: ' + | ||
port); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | ||
'options.port', | ||
['number', 'string'], | ||
port); | ||
} | ||
if (!isLegalPort(port)) { | ||
throw new RangeError('"port" option should be >= 0 and < 65536: ' + port); | ||
throw new errors.RangeError('ERR_SOCKET_BAD_PORT', port); | ||
} | ||
} | ||
port |= 0; | ||
|
@@ -1065,7 +1071,10 @@ function lookupAndConnect(self, options) { | |
} | ||
|
||
if (options.lookup && typeof options.lookup !== 'function') | ||
throw new TypeError('"lookup" option should be a function'); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should likely be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are thrown errors for all the following 4 values in
So I guess that for consistency, all the errors thrown for invalid values of the above vars should have a code of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked how we use mainly use this right now in the code base and it seems like |
||
'options.lookup', | ||
'function', | ||
options.lookup); | ||
|
||
var dnsopts = { | ||
family: options.family, | ||
|
@@ -1207,7 +1216,10 @@ function Server(options, connectionListener) { | |
this.on('connection', connectionListener); | ||
} | ||
} else { | ||
throw new TypeError('options must be an object'); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | ||
'options', | ||
'object', | ||
options); | ||
} | ||
|
||
this._connections = 0; | ||
|
@@ -1465,7 +1477,7 @@ Server.prototype.listen = function(...args) { | |
var backlog; | ||
if (typeof options.port === 'number' || typeof options.port === 'string') { | ||
if (!isLegalPort(options.port)) { | ||
throw new RangeError('"port" argument must be >= 0 and < 65536'); | ||
throw new errors.RangeError('ERR_SOCKET_BAD_PORT', options.port); | ||
} | ||
backlog = options.backlog || backlogFromArgs; | ||
// start TCP server listening on host:port | ||
|
@@ -1490,7 +1502,9 @@ Server.prototype.listen = function(...args) { | |
return this; | ||
} | ||
|
||
throw new Error('Invalid listen argument: ' + util.inspect(options)); | ||
throw new errors.Error('ERR_INVALID_OPT_VALUE', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we probably need a new error for this or change This applies to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EDIT: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We kind of stretched the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pmatzavin I think a new error would not be too specific because neither There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joyeecheung after looking at this further I somewhat agree with you that it is not very intuitive as it is right now. Even though the original error was not really any better. But our current errors mainly reflect the code they are executed in. So most messages are e.g. a clear 1-to-1 of the argument name that is used in the code. The user will have to check that line of code from time to time to understand what the explicit error stands for and this might be such a case. I am not certain that finding a good solution for this should hold up the PR and I personally think we should try to rework the errors in general as soon as they are all ported over. |
||
'options', | ||
util.inspect(options)); | ||
}; | ||
|
||
function lookupAndListen(self, port, address, backlog, exclusive) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error type must also be able to handle the provided port.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made a change to include the port in the err msg if the port arg is passed to the error constructor.