Uh oh!
There was an error while loading. Please reload this page.
fs: fix incorrect error message in fs.open family when flags are not string or int - #2873
fs: fix incorrect error message in fs.open family when flags are not string or int#2873ronkorving wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Note: for async functions this now passes the error to a callback.
There was a problem hiding this comment.
unless flag could ever be zero, i'd just go
if(!intFlags){There was a problem hiding this comment.
To be honest, I'm not sure if it could ever be zero... I could also do a Number.isInteger test, but I figured this would execute faster.
bnoordhuis
commented
Sep 15, 2015
To stop application bugs from propagating, the rule of thumb has always been "throw on logic error, callback on run-time error." Passing in bad flags or options is a logic error and should fail synchronously. |
reqshark
commented
Sep 15, 2015
in that case just change |
ronkorving
commented
Sep 15, 2015
@bnoordhuis I was not familiar with that philosophy. |
reqshark
commented
Sep 15, 2015
Ok don't change the text there then.
.. sounds to me like return an err before even fs.open=function(path,flags,mode,callback_){varcallback=makeCallback(arguments[arguments.length-1]);mode=modeNum(mode,0o666);if(!nullCheck(path,callback))return;varreq=newFSReqWrap();req.oncomplete=callback;varintFlags=stringToFlags(flags);if(intFlags===undefined){returnthrowFlagsError(flags,callback);}binding.open(pathModule._makeLong(path),intFlags,mode,req);}; |
ronkorving
commented
Sep 15, 2015
I'll update this PR soon. |
ronkorving
commented
Sep 15, 2015
@bnoordhuis now it sticks to what you explained. |
There was a problem hiding this comment.
You can replace the try/catch block with an assert.throws(...)
There was a problem hiding this comment.
I wanted to make sure it's a TypeError and not the Error of "bad flag value" (which can also happen when it's a string, just not the right one).
There was a problem hiding this comment.
Understood. If you insist, I'll change it, but I would rather stick to a class check here, to allow other contributions to Node to polish error messages when needed without breaking tests left and right. imho tests shouldn't check for the human readable output when avoidable, and in this case it is avoidable.
There was a problem hiding this comment.
I second brendan's suggestion. What if TypeError is thrown because of some other problem? I prefer checking the actual message.
I would say, when the mesaages are updated, let the tests also be updated.
There was a problem hiding this comment.
Understood. I'll change it.
bnoordhuis
commented
Sep 15, 2015
Basic premise LGTM. |
d405e03 to
163b012CompareThere was a problem hiding this comment.
We can simply this a little bit,
fs.open('/path/to/file/that/does/not/exist',{bad: 'flags'},assert.fail);thefourtheye
commented
Sep 16, 2015
LGTM, with take-it or leave-it suggetions |
Before, it would show "TypeError: flags must be an int", which in native Node is technically correct. But in JS they may be (and usually are) strings. Fixesnodejs#2871
ronkorving
commented
Sep 16, 2015
I liked your suggestions :) |
thefourtheye
commented
Sep 16, 2015
@ronkorving Glad you liked them :-) Let's give this some time so that we can get more reviewers see this (hopefully ;-)). |
There was a problem hiding this comment.
You can condense this to assert.throws(function() { ... }, /File open flags may only be string or integer/).
There was a problem hiding this comment.
Then I wouldn't be checking that it's a TypeError. That's not a problem?
bnoordhuis
commented
Sep 16, 2015
LGTM |
There was a problem hiding this comment.
I don't think you want to check if the error is a TypeError. That should be unconditional, right?
There was a problem hiding this comment.
Well I wrote the error as a TypeError since its thrown on bad types only, so might as well test for it, no?
There was a problem hiding this comment.
Let me rephrase. You should assert that the error is a TypeError, not put it in an if statement.
There was a problem hiding this comment.
Please see my comment below to @bnoordhuis. I can't seem to satisfy both your requests.
cjihrig
commented
Sep 16, 2015
LGTM with one comment. |
There was a problem hiding this comment.
Anyone who sees this message and looks at the docs would immediately report think this a bug, since it conflicts with the docs. If you want to say that, I suggest documenting the behaviour.
I strongly suspect that the fact that flags can be an int is historical, kept for backwards compat from the v0.small days, and should be deprecated, but its hard to deprecate a piece of an API.
This fixes#2871