-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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/fs: convert to using internal/errors #15043
Conversation
lib/fs.js
Outdated
typeof options + ' instead.'); | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | ||
'options', | ||
['object', 'string'], |
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.
'object', 'string' [](start = 32, length = 18)
can you have it ['string', 'object']
just to match the earlier error message?
lib/fs.js
Outdated
@@ -128,7 +131,10 @@ function makeCallback(cb) { | |||
} | |||
|
|||
if (typeof cb !== 'function') { | |||
throw new TypeError('"callback" argument must be a function'); | |||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', |
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.
Please use ERR_INVALID_CALLBACK
here :-)
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.
And every where that the callback error is emitted :-)
looks good in general with a few edits necessary! |
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.
Suggested some improvments
@@ -156,8 +165,11 @@ function makeStatsCallback(cb) { | |||
|
|||
function nullCheck(path, callback) { | |||
if (('' + path).indexOf('\u0000') !== -1) { | |||
var er = new Error('Path must be a string without null bytes'); | |||
er.code = 'ENOENT'; | |||
const er = new errors.Error('ERR_INVALID_ARG_TYPE', |
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.
IMHO this could be changed to TypeError
.
Feedback anyone?
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.
That's really difficult to say. The coercion to a string means that any value can be used, it's not really a TypeError
. The real check here is the null-character bit, which doesn't feel like a TypeError
to me. I could live with it tho.
@@ -1193,7 +1208,10 @@ function toUnixTimestamp(time) { | |||
// convert to 123.456 UNIX timestamp | |||
return time.getTime() / 1000; | |||
} | |||
throw new Error('Cannot parse time: ' + time); | |||
throw new errors.Error('ERR_INVALID_ARG_TYPE', |
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.
- These a disparity with the docs (but out of scope for this PR)
- AFAICT
date string
will not parse
tl;dr 3rd arg should be ['Date', 'time in seconds']
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.
2.You are right I will go with ['Date', 'time in seconds']
The date string was (inaccurate) for a string timestamp arg like '1504020561954'
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 updated it to ['Date', 'time in seconds']
(commit: 2f0738a)
lib/fs.js
Outdated
@@ -1495,7 +1513,10 @@ fs.watchFile = function(filename, options, listener) { | |||
} | |||
|
|||
if (typeof listener !== 'function') { | |||
throw new Error('"watchFile()" requires a listener function'); | |||
throw new errors.Error('ERR_INVALID_ARG_TYPE', |
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.
As above, maybe TypeError
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 updated it to TypeError
(commit: 2f0738a)
lib/fs.js
Outdated
@@ -1903,16 +1932,16 @@ function ReadStream(path, options) { | |||
|
|||
if (this.start !== undefined) { | |||
if (typeof this.start !== 'number') { | |||
throw new TypeError('"start" option must be a Number'); | |||
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'start', this.start); |
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.
IMHO ERR_INVALID_ARG_TYPE
might be better.
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 updated it to ERR_INVALID_ARG_TYPE
(commit: 2f0738a)
lib/fs.js
Outdated
} | ||
if (this.end === undefined) { | ||
this.end = Infinity; | ||
} else if (typeof this.end !== 'number') { | ||
throw new TypeError('"end" option must be a Number'); | ||
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'end', this.end); |
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.
Same as L1935
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 updated it to ERR_INVALID_ARG_TYPE
(commit: 2f0738a)
lib/fs.js
Outdated
} | ||
|
||
if (this.start > this.end) { | ||
throw new Error('"start" option must be <= "end" option'); | ||
throw new errors.Error('ERR_INVALID_OPT_VALUE', 'end', this.end); |
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.
IMHO ERR_VALUE_OUT_OF_RANGE
might be better:
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE',
'start',
'<= "end"',
`{start: ${this.start}, end: ${this.end}}`);
to produce:
The value of "start" must be <= "end". Received "{start: 5, end: 4}"
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 updated it to ERR_VALUE_OUT_OF_RANGE
and I added the new error ERR_VALUE_OUT_OF_RANGE
in errors.js
(commit: 2f0738a)
lib/fs.js
Outdated
@@ -2069,10 +2098,10 @@ function WriteStream(path, options) { | |||
|
|||
if (this.start !== undefined) { | |||
if (typeof this.start !== 'number') { | |||
throw new TypeError('"start" option must be a Number'); | |||
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'start', this.start); |
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.
Same as L1935
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 updated it to ERR_INVALID_ARG_TYPE
(commit: 2f0738a)
lib/fs.js
Outdated
} | ||
if (this.start < 0) { | ||
throw new Error('"start" must be >= zero'); | ||
throw new errors.Error('ERR_INVALID_OPT_VALUE', 'start', this.start); |
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.
Similar to L1944
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 updated it to ERR_VALUE_OUT_OF_RANGE
(commit: 2f0738a)
}, /"start" option must be <= "end" option/); | ||
common.expectsError( | ||
() => { | ||
fs.createReadStream(rangeFile, { start: 10, end: 2 }); |
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.
Just for this case I suggest validating the output message as well
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 added the message assertion (commit: 2f0738a)
Hello @pmatzavin and welcome (or is it welcome back?). Thank you for tackling this one, it's a non trivial change 🥇 P.S. If you have any question you can also feel free to contact me directly. |
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.
💯
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.
LGTM thanks for helping with the error conversion.
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.
LGTM with green CI
covert lib/fs.js over to using lib/internal/errors.js i have not addressed the cases that use errnoException(), for reasons described in nodejsGH-12926 - throw the ERR_INVALID_CALLBACK error when the the callback is invalid - replace the ['object', 'string'] with ['string', 'object'] in the error constructor call, to better match the previous err msg in the getOptions() function - add error ERR_VALUE_OUT_OF_RANGE in lib/internal/errors.js, this error is thrown when a numeric value is out of range - document the ERR_VALUE_OUT_OF_RANGE err in errors.md - correct the expected args, in the error thrown in the function fs._toUnixTimestamp() to ['Date', 'time in seconds'] (lib/fs.js) - update the listener error type in the fs.watchFile() function, from Error to TypeError (lib/fs.js) - update errors from ERR_INVALID_OPT_VALUE to ERR_INVALID_ARG_TYPE in the functions fs.ReadStream() and fs.WriteStream(), for the cases of range errors use the new error: ERR_VALUE_OUT_OF_RANGE (lib/fs.js) PR-URL: nodejs#15043 Refs: nodejs#11273 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
2f0738a
to
0b1285c
Compare
Pre-land sanity: https://ci.nodejs.org/job/node-test-commit-linuxone/8335/ |
covert lib/fs.js over to using lib/internal/errors.js i have not addressed the cases that use errnoException(), for reasons described in nodejsGH-12926 - throw the ERR_INVALID_CALLBACK error when the the callback is invalid - replace the ['object', 'string'] with ['string', 'object'] in the error constructor call, to better match the previous err msg in the getOptions() function - add error ERR_VALUE_OUT_OF_RANGE in lib/internal/errors.js, this error is thrown when a numeric value is out of range - document the ERR_VALUE_OUT_OF_RANGE err in errors.md - correct the expected args, in the error thrown in the function fs._toUnixTimestamp() to ['Date', 'time in seconds'] (lib/fs.js) - update the listener error type in the fs.watchFile() function, from Error to TypeError (lib/fs.js) - update errors from ERR_INVALID_OPT_VALUE to ERR_INVALID_ARG_TYPE in the functions fs.ReadStream() and fs.WriteStream(), for the cases of range errors use the new error: ERR_VALUE_OUT_OF_RANGE (lib/fs.js) PR-URL: nodejs#15043 Refs: nodejs#11273 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
Landed in 219932a |
covert lib/fs.js over to using lib/internal/errors.js i have not addressed the cases that use errnoException(), for reasons described in GH-12926 - throw the ERR_INVALID_CALLBACK error when the the callback is invalid - replace the ['object', 'string'] with ['string', 'object'] in the error constructor call, to better match the previous err msg in the getOptions() function - add error ERR_VALUE_OUT_OF_RANGE in lib/internal/errors.js, this error is thrown when a numeric value is out of range - document the ERR_VALUE_OUT_OF_RANGE err in errors.md - correct the expected args, in the error thrown in the function fs._toUnixTimestamp() to ['Date', 'time in seconds'] (lib/fs.js) - update the listener error type in the fs.watchFile() function, from Error to TypeError (lib/fs.js) - update errors from ERR_INVALID_OPT_VALUE to ERR_INVALID_ARG_TYPE in the functions fs.ReadStream() and fs.WriteStream(), for the cases of range errors use the new error: ERR_VALUE_OUT_OF_RANGE (lib/fs.js) PR-URL: nodejs/node#15043 Refs: nodejs/node#11273 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
covert lib/fs.js over to using lib/internal/errors.js
ref: #11273
i have not addressed the cases that use errnoException(),
for reasons described in GH-12926
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes(https://github.com/nodejs/node/blob/master/CONTRIBUTING.md#commit-message-guidelines)
Affected core subsystem(s)
lib/fs