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

errors: more specific error if sysctl not found #21

Merged
merged 1 commit into from
Jan 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ impl CtlInfo {

#[derive(Debug, Fail)]
pub enum SysctlError {
#[fail(display = "no such sysctl: {}", _0)]
NotFound(String),

#[fail(display = "no matching type for value")]
#[cfg(not(target_os = "macos"))]
UnknownType,
Expand Down Expand Up @@ -434,7 +437,11 @@ fn name2oid(name: &str) -> Result<Vec<c_int>, SysctlError> {
)
};
if ret < 0 {
return Err(SysctlError::IoError(io::Error::last_os_error()));
let e = io::Error::last_os_error();
return Err(match e.kind() {
std::io::ErrorKind::NotFound => SysctlError::NotFound(name.into()),
_ => SysctlError::IoError(e),
});
}

// len is in bytes, convert to number of c_ints
Expand Down Expand Up @@ -1437,6 +1444,9 @@ impl Ctl {
///
/// This is just a wrapper around `Ctl::from_str`.
///
/// Returns a result containing the struct Ctl on success or a SysctlError
/// on failure.
///
/// # Example
///
/// ```
Expand All @@ -1445,6 +1455,19 @@ impl Ctl {
///
/// let ctl = Ctl::new("kern.osrelease");
/// ```
///
/// If the sysctl does not exist, `Err(SysctlError::NotFound)` is returned.
/// ```
/// extern crate sysctl;
/// use sysctl::{Ctl, SysctlError};
///
/// let ctl = Ctl::new("this.sysctl.does.not.exist");
/// match ctl {
/// Err(SysctlError::NotFound(_)) => (),
/// Err(_) => panic!("Wrong error type returned"),
/// Ok(_) => panic!("Nonexistent sysctl seems to exist"),
/// }
/// ```
pub fn new(name: &str) -> Result<Self, SysctlError> {
Ctl::from_str(name)
}
Expand Down