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

Add full chmod support #18

Merged
merged 3 commits into from
Oct 18, 2023
Merged

Add full chmod support #18

merged 3 commits into from
Oct 18, 2023

Conversation

timriley
Copy link
Member

This was already supported inside the memory file system adapter. Update the main file system adapter to support this, and expose a public method.

This was already supported inside the memory file system adapter. Update the main file system adapter to support this, and expose a public method.
Copy link
Member

@jodosha jodosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timriley Please have a look at my inline comment about the exception class. 🙂

lib/dry/files.rb Outdated
# @since @1.1.0
# @api public
def chmod(path, mode)
raise ArgumentError, "file mode should be an integer" unless mode.is_a?(Integer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ArgumentError, "file mode should be an integer" unless mode.is_a?(Integer)
raise Dry::Files::Error, "file mode should be an integer" unless mode.is_a?(Integer)

@timriley From a user perspective, when I'm using dry-files, I may want to catch all the possible errors that the lib may raise. See the following example:

begin
  # some dry-files operation
rescue Dry::Files::Error
  # handle the error
end

It starts to become cumbersome for users if we raise errors that aren't Dry::Files::Error subclasses.

Copy link
Member Author

@timriley timriley Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jodosha I agree with you here, but do you think this rule should apply for things like argument errors?

I definitely agree that having a base error class makes sense for "something has gone wrong while executing the file operation".

But "you made a programmer mistake and passed an incorrect argument" feels like a different category of error, and I would not want that to be caught by that rescue Dry::Files::Error example above, because I'd want to see the uncaught exception and use the feedback from that error to correct my mistake. (In this case, I could imagine someone passing "+x" and then learning from the exception that we support the integer mode only).

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timriley

I would not want that to be caught by that

But how can a user know about all the possible combinations?
The only safer way for them would be to catch StandardError, which is too broad.

Remember that we're talking about handling errors, not suppressing them.
For simplicity's sake, we should stick to the rule of one superclass for raised errors.

We can also introduce a specific subclass for this case.

Copy link
Member Author

@timriley timriley Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jodosha as the primary author of this gem, I'll defer to your judgement here. I'll update this to return a Dry::Files::Error and the merge this PR.

But before I do that, let me add one more thought about what I was trying to explain earlier 😜


The way I see this ArgumentError here is that it's equivalent to e.g. the user forgetting even to provide a second argument in the first place. It's just that we're checking for the nature of the second argument in addition to its presence.

So a user forgetting a second argument is still an ArgumentError, and by the principle of "dry-files method errors should all be rescuable by a single error type", shouldn't we rescue for ArgumentErrors raised by Ruby itself and turn them into Dry::Files::Error? No, of course we shouldn't, because that would be silly: it goes against the grain of Ruby and would be unpredictable for the user.

In the case of an ArgumentError due to a missing argument, it's a signal that the user isn't using the API right, and they want to know about it so they can correct it, and then let any subsequent Dry::Files::Error exceptions signal that something external has gone wrong even after their correct calls to the methods.

I see this custom ArgumentError as another such case, and something the user wants to know about (through an uncaught exception) as early as possible, so they can correct their usage of the API and then get onto seeing the real meaningful errors that may come afterwards.


Anyway, if for any reason this changes your thinking, we can always adjust the error again afterwards. But in the spirit of just getting on with things, this is the last I'll say of it 😄

Thanks, as always, for listening ❤️

@timriley
Copy link
Member Author

@jodosha This just needs a re-review!

Copy link
Member

@jodosha jodosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timriley LGTM 👍

lib/dry/files.rb Outdated Show resolved Hide resolved
lib/dry/files/file_system.rb Outdated Show resolved Hide resolved
@timriley timriley merged commit 4ce8303 into main Oct 18, 2023
10 checks passed
@timriley timriley deleted the add-chmod-support-to-file-system branch October 18, 2023 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants