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

lib/fs: convert to using internal/errors #15043

Merged
merged 1 commit into from Aug 31, 2017
Merged

Conversation

@pmatzavin
Copy link
Contributor

@pmatzavin pmatzavin commented Aug 26, 2017

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
Affected core subsystem(s)

lib/fs

lib/fs.js Outdated
typeof options + ' instead.');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'options',
['object', 'string'],

This comment has been minimized.

@kunalspathak

kunalspathak Aug 28, 2017
Member

'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',

This comment has been minimized.

@jasnell

jasnell Aug 29, 2017
Member

Please use ERR_INVALID_CALLBACK here :-)

This comment has been minimized.

@jasnell

jasnell Aug 29, 2017
Member

And every where that the callback error is emitted :-)

@jasnell
Copy link
Member

@jasnell jasnell commented Aug 29, 2017

looks good in general with a few edits necessary!

Copy link
Member

@refack refack left a comment

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',

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

IMHO this could be changed to TypeError.
Feedback anyone?

This comment has been minimized.

@jasnell

jasnell Aug 29, 2017
Member

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',

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

  1. These a disparity with the docs (but out of scope for this PR)
  2. AFAICT date string will not parse

tl;dr 3rd arg should be ['Date', 'time in seconds']

This comment has been minimized.

@pmatzavin

pmatzavin Aug 29, 2017
Author Contributor

2.You are right I will go with ['Date', 'time in seconds']
The date string was (inaccurate) for a string timestamp arg like '1504020561954'

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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',

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

As above, maybe TypeError

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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);

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

IMHO ERR_INVALID_ARG_TYPE might be better.

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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);

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

Same as L1935

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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);

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

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}"

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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);

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

Same as L1935

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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);

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

Similar to L1944

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

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 });

This comment has been minimized.

@refack

refack Aug 29, 2017
Member

Just for this case I suggest validating the output message as well

This comment has been minimized.

@pmatzavin

pmatzavin Aug 30, 2017
Author Contributor

I added the message assertion (commit: 2f0738a)

@refack
Copy link
Member

@refack refack commented Aug 29, 2017

Hello @pmatzavin and welcome (or is it welcome back?). Thank you for tackling this one, it's a non trivial change 🥇
If you haven't already, it's recommended you take a look at CONTRIBUTING.md guide (especially the part about "discuss and update"). Customarily PRs are kept open for at least 48 hours so that anyone interested gets a chance to comment or review. Also internal/error migrations are "semver-major" so they require 2 approvals from CTC members, so it might take longer (hopefully not).

P.S. If you have any question you can also feel free to contact me directly.

@refack refack self-assigned this Aug 29, 2017
@jasnell jasnell dismissed their stale review Aug 29, 2017

I hadn't meant to hit approve yet!

@refack
refack approved these changes Aug 30, 2017
Copy link
Member

@refack refack left a comment

💯

Copy link
Member

@mhdawson mhdawson left a comment

LGTM thanks for helping with the error conversion.

Copy link
Member

@jasnell jasnell left a comment

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 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: #15043
Refs: #11273
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
@refack refack force-pushed the pmatzavin:lib_fs_convert_err branch to 0b1285c Aug 31, 2017
@refack refack merged commit 0b1285c into nodejs:master Aug 31, 2017
1 check was pending
1 check was pending
test/linux-one running tests
Details
refack added a commit to refack/node that referenced this pull request Aug 31, 2017
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 <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
@refack
Copy link
Member

@refack refack commented Aug 31, 2017

Landed in 219932a
(force pushed)

@pmatzavin pmatzavin deleted the pmatzavin:lib_fs_convert_err branch Sep 2, 2017
addaleax added a commit to addaleax/ayo that referenced this pull request Sep 5, 2017
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 <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
@refack refack removed their assignment Oct 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants
You can’t perform that action at this time.