Skip to content

Commit

Permalink
errors: hide internal error types from ErrorKind
Browse files Browse the repository at this point in the history
As we plan to export ErrorKind, these kinds of internal errors should be
made opaque since it seems incredibly unlikely that any real users would
ever need to check them.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
  • Loading branch information
cyphar committed Dec 7, 2024
1 parent 5254674 commit d99986c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ pub(crate) enum ErrorKind {
NotSupported,
InvalidArgument,
SafetyViolation,
BadSymlinkStack,
ParseError,
InternalError,
// TODO: We might want to use Option<std::io::ErrorKind>?
OsError(Option<i32>),
}
Expand All @@ -121,12 +120,16 @@ impl ErrorImpl {
Self::NotSupported { .. } => ErrorKind::NotSupported,
Self::InvalidArgument { .. } => ErrorKind::InvalidArgument,
Self::SafetyViolation { .. } => ErrorKind::SafetyViolation,
Self::BadSymlinkStackError { .. } => ErrorKind::BadSymlinkStack,
Self::ParseIntError(_) => ErrorKind::ParseError,
// Any syscall-related errors get mapped to an OsError, since the
// distinction doesn't matter to users checking error values.
Self::OsError { source, .. } => ErrorKind::OsError(source.raw_os_error()),
Self::RawOsError { source, .. } => {
ErrorKind::OsError(source.root_cause().raw_os_error())
}
// These errors are internal error types that we don't want to
// expose outside of the crate. All that matters to users is that
// there was some internal error.
Self::BadSymlinkStackError { .. } | Self::ParseIntError(_) => ErrorKind::InternalError,
Self::Wrapped { source, .. } => source.kind(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/capi/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ErrorImpl for CapiError {
// TODO: We should probably have an actual "no-op" error here that
// is unused except for these tests so we can properly detect
// a bad ErrorKind.
ErrorKind::ParseError
ErrorKind::InternalError
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod tests {
super::sysctl_read_parse::<u32>(&GLOBAL_PROCFS_HANDLE, "kernel.printk")
.as_ref()
.map_err(Error::kind),
Err(ErrorKind::ParseError),
Err(ErrorKind::InternalError),
"parsing line from multi-number sysctl",
);
}
Expand All @@ -126,7 +126,7 @@ mod tests {
super::sysctl_read_parse::<u32>(&GLOBAL_PROCFS_HANDLE, "kernel.random.uuid")
.as_ref()
.map_err(Error::kind),
Err(ErrorKind::ParseError),
Err(ErrorKind::InternalError),
"parsing line from non-number sysctl",
);
}
Expand Down

0 comments on commit d99986c

Please sign in to comment.