-
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
fs: fix confusing flags TypeError msg #2902
Conversation
return flag; | ||
|
||
if (typeof flag !== 'string') { | ||
throw new TypeError('flag must be a 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.
Suggestion: drop the braces for consistency or add them above.
LGTM |
File open flags must be an int when passed to the binding layer, but they must be a string when passed to the fs module (numbers are permitted, though undocumented). The module used to do no type checking, so the binding layer error would be thrown, and it was wrong: > fs.openSync('_') TypeError: flags must be an int at TypeError (native) at Object.fs.openSync (fs.js:549:18) It is now: > fs.openSync('_') TypeError: flag must be a string PR-URL: nodejs#2902 Reviewed-By: Ben Noordhuis <[email protected]>
e097466
to
493864a
Compare
CI pending: https://ci.nodejs.org/job/node-test-pull-request/319/ Braces dropped. |
So why not just add the tests (or ask me for them)? Personally, I think the error you now throw is incorrect (ints are fine, not just strings), and the check is incorrect too ( |
@ronkorving yea but the number returns flag before the string check. this looks good to me |
oh wait now i see what you're saying nevermind it doesn't look that good, you're right the check is wrong |
this whole thing would be a lot simpler if number weren't permitted. why are numbers OK at the JS layer? |
I really like those numerical constants though. I would love to see them exposed and documented. |
@ronkorving feel free to grab bits of this and merge it into your PR
This code pre-existed your PR, and I think it addresses most of the comments on yours. But feel free to keep going on yours.
Run the code. If you pass |
@sam-github good call. I'm seeing the same |
@sam-github thanks for the explanation. So we both fixed the same issue (which wasn't reported yet) in parallel? Neato :) Anyway, your solution kinda works for me, thanks for explaining. However, if we really don't want people to pass integers, why allow it at all? Your number type check works, but still, the native TypeError will ask explicitly for integers, while that's still not what we want to convey to users... So should we allow numbers at all? I like those constants, I have to admit, but they're not exposed nor documented and perhaps it's better that way. So perhaps just throw a TypeError if a non-string (even numbers) is passed, period? In any case, I'll close my PR. |
See also: #5590 |
Closing because #5590 landed. |
👍 |
Fix #2871, replace #2873. I did this on the plane back from NodeConf, it is basically the same change as #2873, but with more tests.
File open flags must be an int when passed to the binding layer, but
they must be a string when passed to the fs module (numbers are
permitted, though undocumented). The module used to do no type checking,
so the binding layer error would be thrown, and it was wrong:
It is now: