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

Refactor error types #67

Merged
merged 1 commit into from
Jul 18, 2024
Merged

Refactor error types #67

merged 1 commit into from
Jul 18, 2024

Conversation

robin-nitrokey
Copy link
Member

This PR splits the Error enum into an Error struct and a non-exhaustive ErrorKind enum. This makes it possible to add support for new error codes without breaking compatibility. The PR also removes the edge case of Error instances for non-negative return values indicating success.

See this issue for context and discussion:

Contrary to the suggestion in #55 (comment), this implementation does not provide a fn error(&self) -> Option<ErrorKind> on Error. It would again cause the problem that users could rely on this method returning None for some currently unsupported error. Instead, Error implements PartialEq<ErrorKind> so that users can compare it directly against known error kinds. The only limitation is that it is not possible to match on the error kind, but I don’t see a need for that anyway.

src/io.rs Outdated Show resolved Hide resolved
src/io.rs Outdated Show resolved Hide resolved
src/fs.rs Outdated
@@ -1254,23 +1254,19 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> {
let path_slice = path.as_ref().as_bytes();
for i in 0..path_slice.len() {
if path_slice[i] == b'/' {
let dir = PathBuf::try_from(&path_slice[..i])?;
let dir = PathBuf::try_from(&path_slice[..i]).map_err(|_| ErrorKind::Io)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be ErrorKind::NameTooLong?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not want to change the existing behavior, but I think this cannot fail anyway. If we would have a proper split method in Path, we could get rid of the error entirely.

@robin-nitrokey
Copy link
Member Author

If the constants on Error are public, we don’t even need the ErrorKind enum, do we?

@sosthene-nitrokey
Copy link
Contributor

That's a good point.

We can re-introduce ErrorKind in the future if we ever need it.

@robin-nitrokey
Copy link
Member Author

Okay. Then I would change the constant names to match the old variant names, e. g. NO_SUCH_ENTRY instead of NOENT.

To make it easier to add support for new error codes without breaking
compatibility, this patch replaces the Error enum with a struct and
associated constants.  As a part of this change, it also enforces that
Error is not used for return values indicating success, i. e. only for
negative return values.
@robin-nitrokey robin-nitrokey merged commit ee539c7 into main Jul 18, 2024
8 checks passed
@robin-nitrokey robin-nitrokey deleted the errors branch July 18, 2024 08:00
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.

3 participants