diff --git a/Cargo.lock b/Cargo.lock index 13cf0836e9..7d7f6b5fd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1051,6 +1051,7 @@ dependencies = [ "serde_json", "sync", "tempfile", + "thiserror", ] [[package]] diff --git a/sys_util/Cargo.toml b/sys_util/Cargo.toml index 7edffd9f58..8274aac10e 100644 --- a/sys_util/Cargo.toml +++ b/sys_util/Cargo.toml @@ -9,6 +9,7 @@ include = ["src/**/*", "Cargo.toml"] data_model = { path = "../data_model" } # provided by ebuild libc = "*" poll_token_derive = { version = "*", path = "poll_token_derive" } +thiserror = "*" serde = { version = "1", features = [ "derive" ] } serde_json = "1" sync = { path = "../sync" } # provided by ebuild diff --git a/sys_util/src/syslog.rs b/sys_util/src/syslog.rs index a20db926fe..eefcd319f9 100644 --- a/sys_util/src/syslog.rs +++ b/sys_util/src/syslog.rs @@ -33,6 +33,7 @@ use std::path::PathBuf; use std::sync::{MutexGuard, Once}; use sync::Mutex; +use thiserror::Error as ThisError; /// The priority (i.e. severity) of a syslog message. /// @@ -93,37 +94,28 @@ pub enum Facility { } /// Errors returned by `syslog::init()`. -#[derive(Debug)] +#[derive(ThisError, Debug)] pub enum Error { /// Initialization was never attempted. + #[error("initialization was never attempted")] NeverInitialized, /// Initialization has previously failed and can not be retried. + #[error("initialization previously failed and cannot be retried")] Poisoned, /// Error while creating socket. + #[error("failed to create socket: {0}")] Socket(io::Error), /// Error while attempting to connect socket. + #[error("failed to connect socket: {0}")] Connect(io::Error), - // There was an error using `open` to get the lowest file descriptor. + /// There was an error using `open` to get the lowest file descriptor. + #[error("failed to get lowest file descriptor: {0}")] GetLowestFd(io::Error), - // The guess of libc's file descriptor for the syslog connection was invalid. + /// The guess of libc's file descriptor for the syslog connection was invalid. + #[error("guess of fd for syslog connection was invalid")] InvalidFd, } -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - match self { - NeverInitialized => write!(f, "initialization was never attempted"), - Poisoned => write!(f, "initialization previously failed and cannot be retried"), - Socket(e) => write!(f, "failed to create socket: {}", e), - Connect(e) => write!(f, "failed to connect socket: {}", e), - GetLowestFd(e) => write!(f, "failed to get lowest file descriptor: {}", e), - InvalidFd => write!(f, "guess of fd for syslog connection was invalid"), - } - } -} - fn get_proc_name() -> Option { env::args_os() .next()