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

Use std::io::Error instead of own error type #2

Merged
merged 4 commits into from
Feb 19, 2020

Conversation

LPGhatguy
Copy link
Collaborator

Closes #1.

This PR converts the crate's public functions to return std::io::Error instead of a custom error type. I introduced a new custom error type that's internal to the crate and then updated all interfaces to use it.

This is a breaking change!

|path| format!("failed to read from file `{}`", path),
)
.map_err(Error::into_io_error)
(&(**self).file)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I only learned that there are trait implementations for &File today and I struggled to get this re-borrow to work. Is there a cleaner way to do this that you know?

Ok(result) => Ok(result),
Err(error) => Err(Error::new(error, context(&self.path.display()))),
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I replaced try_exec with just using map_err across the board

Copy link
Owner

Choose a reason for hiding this comment

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

The reason for try_exec was to let each function provide a custom error message based on the operation that failed - I would prefer not to throw that away

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see! I've always thought of errors missing paths as the only issue with Rust's std FS errors, but they also don't include what operation they tried to perform.

Do you think it would make sense to re-introduce that custom error message by using an enum so that we can avoid allocating/formatting the error message?

Copy link
Owner

Choose a reason for hiding this comment

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

An enum would work, although since we need to clone the PathBuf already I don't think theres much advantage over just using format! directly

#[derive(Debug)]
pub(crate) struct Error {
source: io::Error,
path: PathBuf,
Copy link
Owner

Choose a reason for hiding this comment

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

As I mentioned in my other comment, this should have some kind of context about what operation failed

I'm also not a fan of printing the source error message in the Display impl because a generic consumer who just iterates over the error causes will end up repeating the error message like

failed to get configuration
caused by: No such file or directory (os error 2) in /my/file
caused by: No such file or directory (os error 2)

@LPGhatguy
Copy link
Collaborator Author

I added more specific error messages back to each error variant. I tried the enum approach, it has the nice advantage that error messages aren't duplicated and helps keep the implementation tidy, which I like.

I've also fixed the source-printing issue you mentioned. I think I need to go back through some of my projects and fix the way they print errors...

@andrewhickman andrewhickman merged commit 52bc2f6 into andrewhickman:master Feb 19, 2020
@andrewhickman
Copy link
Owner

Thanks!

@andrewhickman
Copy link
Owner

I've just published version 2.0.0 with these changes

@LPGhatguy LPGhatguy deleted the use-io-error branch February 19, 2020 21:28
@LPGhatguy LPGhatguy mentioned this pull request Feb 19, 2020
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.

Use wrapper around std::io::Error instead of custom error type?
2 participants