diff --git a/src/uu/nohup/Cargo.toml b/src/uu/nohup/Cargo.toml index df3248561..0ca725e6c 100644 --- a/src/uu/nohup/Cargo.toml +++ b/src/uu/nohup/Cargo.toml @@ -20,6 +20,7 @@ path = "src/nohup.rs" clap = { workspace = true } libc = { workspace = true } uucore = { workspace = true, features = ["fs"] } +thiserror = { workspace = true } [[bin]] name = "nohup" diff --git a/src/uu/nohup/src/nohup.rs b/src/uu/nohup/src/nohup.rs index 60ad979bb..00643d488 100644 --- a/src/uu/nohup/src/nohup.rs +++ b/src/uu/nohup/src/nohup.rs @@ -10,11 +10,11 @@ use libc::{c_char, dup2, execvp, signal}; use libc::{SIGHUP, SIG_IGN}; use std::env; use std::ffi::CString; -use std::fmt::{Display, Formatter}; use std::fs::{File, OpenOptions}; use std::io::{Error, IsTerminal}; use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; +use thiserror::Error; use uucore::display::Quotable; use uucore::error::{set_exit_code, UClapError, UError, UResult}; use uucore::{format_usage, help_about, help_section, help_usage, show_error}; @@ -33,15 +33,24 @@ mod options { pub const CMD: &str = "cmd"; } -#[derive(Debug)] +#[derive(Debug, Error)] enum NohupError { + #[error("Cannot detach from console")] CannotDetach, - CannotReplace(&'static str, std::io::Error), - OpenFailed(i32, std::io::Error), - OpenFailed2(i32, std::io::Error, String, std::io::Error), -} -impl std::error::Error for NohupError {} + #[error("Cannot replace {name}: {err}", name = .0, err = .1)] + CannotReplace(&'static str, #[source] Error), + + #[error("failed to open {path}: {err}", path = NOHUP_OUT.quote(), err = .1)] + OpenFailed(i32, #[source] Error), + + #[error("failed to open {first_path}: {first_err}\nfailed to open {second_path}: {second_err}", + first_path = NOHUP_OUT.quote(), + first_err = .1, + second_path = .2.quote(), + second_err = .3)] + OpenFailed2(i32, #[source] Error, String, Error), +} impl UError for NohupError { fn code(&self) -> i32 { @@ -52,26 +61,6 @@ impl UError for NohupError { } } -impl Display for NohupError { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - match self { - Self::CannotDetach => write!(f, "Cannot detach from console"), - Self::CannotReplace(s, e) => write!(f, "Cannot replace {s}: {e}"), - Self::OpenFailed(_, e) => { - write!(f, "failed to open {}: {}", NOHUP_OUT.quote(), e) - } - Self::OpenFailed2(_, e1, s, e2) => write!( - f, - "failed to open {}: {}\nfailed to open {}: {}", - NOHUP_OUT.quote(), - e1, - s.quote(), - e2 - ), - } - } -} - #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args).with_exit_code(125)?;