Better errors in summary

This commit is contained in:
Zykino 2023-01-28 22:24:39 +01:00
parent 258d426ffd
commit 6769376ae2
4 changed files with 62 additions and 50 deletions

View File

@ -109,7 +109,7 @@ impl Updater {
// self.systems.iter().collect(); // self.systems.iter().collect();
let mut systems: Vec<_> = vec![]; let mut systems: Vec<_> = vec![];
for sys in &self.systems { for sys in &self.systems {
systems.push(self.update(&sys, opt)); systems.push(self.update(&sys, opt).into());
} }
Summary { systems } Summary { systems }
} }
@ -145,10 +145,10 @@ impl System {
let cmd = fetch.clone().prepare(opt); let cmd = fetch.clone().prepare(opt);
let exit_status = cmd let exit_status = cmd
.execute(opt) .execute(opt)
.map_err(|err| MyError::new(ErrorKind::Fetch, err, cmd.clone()))?; .map_err(|err| MyError::new(MyErrorKind::Fetch, err, cmd.clone()))?;
if !exit_status.success() { if !exit_status.success() {
return Err(MyError::new( return Err(MyError::new(
ErrorKind::Fetch, MyErrorKind::Fetch,
io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)),
cmd.clone(), cmd.clone(),
)); ));
@ -162,10 +162,10 @@ impl System {
let cmd = compile.clone().prepare(opt); let cmd = compile.clone().prepare(opt);
let exit_status = cmd let exit_status = cmd
.execute(opt) .execute(opt)
.map_err(|err| MyError::new(ErrorKind::Compile, err, cmd.clone()))?; .map_err(|err| MyError::new(MyErrorKind::Compile, err, cmd.clone()))?;
if !exit_status.success() { if !exit_status.success() {
return Err(MyError::new( return Err(MyError::new(
ErrorKind::Fetch, MyErrorKind::Fetch,
io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)),
cmd.clone(), cmd.clone(),
)); ));
@ -178,10 +178,10 @@ impl System {
let cmd = self.install.clone().prepare(opt); let cmd = self.install.clone().prepare(opt);
let exit_status = cmd let exit_status = cmd
.execute(opt) .execute(opt)
.map_err(|err| MyError::new(ErrorKind::Install, err, cmd.clone()))?; .map_err(|err| MyError::new(MyErrorKind::Install, err, cmd.clone()))?;
if !exit_status.success() { if !exit_status.success() {
return Err(MyError::new( return Err(MyError::new(
ErrorKind::Fetch, MyErrorKind::Fetch,
io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)),
cmd.clone(), cmd.clone(),
)); ));
@ -201,7 +201,7 @@ impl Cmd {
} }
fn prepare(self, opt: &Opt) -> ActualCmd { fn prepare(self, opt: &Opt) -> ActualCmd {
let mut params = self.params; let params = self.params;
let mut env = self.env; let mut env = self.env;
if !env.contains_key("PATH") { if !env.contains_key("PATH") {
@ -254,11 +254,14 @@ impl fmt::Display for ActualCmd {
write!(f, " in {:?}", cdir)?; write!(f, " in {:?}", cdir)?;
} }
if !self.env.is_empty() { Ok(())
writeln!(f, " with the following environment variable:")?;
writeln!(f, "{:#?}", self.env) // // TODO: remove me (too verbose)
} else { // if !self.env.is_empty() {
write!(f, " without any environment variable. ") // writeln!(f, " with the following environment variable:")?;
} // writeln!(f, "{:#?}", self.env)
// } else {
// write!(f, " without any environment variable. ")
// }
} }
} }

View File

@ -1,8 +1,12 @@
use crate::*; use crate::*;
use std::error::Error; use std::error::Error;
use std::fmt::{Display, Formatter, Result}; use std::fmt;
use std::fmt::{Display, Formatter};
use std::io; use std::io;
use std::result;
pub type Result<T> = result::Result<T, MyError>;
/// An error that can occur in this crate. /// An error that can occur in this crate.
/// ///
@ -10,13 +14,13 @@ use std::io;
#[derive(Debug)] #[derive(Debug)]
pub struct MyError { pub struct MyError {
source: io::Error, source: io::Error,
kind: ErrorKind, kind: MyErrorKind,
cmd: ActualCmd, cmd: ActualCmd,
} }
#[derive(Debug)] #[derive(Debug)]
#[non_exhaustive] #[non_exhaustive]
pub enum ErrorKind { pub enum MyErrorKind {
Config, Config,
Fetch, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? Fetch, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error?
Compile, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? Compile, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error?
@ -24,12 +28,12 @@ pub enum ErrorKind {
} }
impl MyError { impl MyError {
pub(crate) fn new(kind: ErrorKind, source: io::Error, cmd: ActualCmd) -> MyError { pub(crate) fn new(kind: MyErrorKind, source: io::Error, cmd: ActualCmd) -> MyError {
MyError { source, kind, cmd } MyError { source, kind, cmd }
} }
/// Return the kind of this error. /// Return the kind of this error.
pub fn kind(&self) -> &ErrorKind { pub fn kind(&self) -> &MyErrorKind {
&self.kind &self.kind
} }
} }
@ -41,28 +45,28 @@ impl Error for MyError {
} }
impl Display for MyError { impl Display for MyError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.kind { match self.kind {
ErrorKind::Config => write!( MyErrorKind::Config => write!(
f, f,
"Could not read configuration file: {}", "Could not read configuration file: {}",
self.source().unwrap() self.source().unwrap()
), ),
ErrorKind::Fetch => write!( MyErrorKind::Fetch => write!(
f, f,
"Could not fetch with command `{}`: {}", "Could not fetch with command {}: {}",
self.cmd, self.cmd,
self.source().unwrap() self.source().unwrap()
), ),
ErrorKind::Compile => write!( MyErrorKind::Compile => write!(
f, f,
"Could not compile with command `{}`: {}", "Could not compile with command {}: {}",
self.cmd, self.cmd,
self.source().unwrap() self.source().unwrap()
), ),
ErrorKind::Install => write!( MyErrorKind::Install => write!(
f, f,
"Could not install with command `{}`: {}", "Could not install with command {}: {}",
self.cmd, self.cmd,
self.source().unwrap() self.source().unwrap()
), ),

View File

@ -7,9 +7,6 @@ use errors::*;
use clap::Parser; use clap::Parser;
use std::fmt::Display; use std::fmt::Display;
use std::path::PathBuf; use std::path::PathBuf;
use std::result;
pub type Result<T> = result::Result<T, MyError>;
#[derive(Parser)] #[derive(Parser)]
pub struct Opt { pub struct Opt {
@ -31,27 +28,36 @@ pub struct Opt {
pub steps: Vec<String>, pub steps: Vec<String>,
} }
// enum State { enum State {
// /// All steps asked were successful /// All steps asked were successful
// Ok, Ok,
// Err(MyError), Err(MyError),
// } }
// impl Display for State { impl Display for State {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// match self { match self {
// State::Ok => write!(f, "Ok"), State::Ok => write!(f, "Ok"),
// State::Err(e) => write!(f, "Error: {}", e), State::Err(e) => write!(f, "Error: {}", e),
// // State::Fetch(e) => write!(f, "Fetch error: {}", e), // State::Fetch(e) => write!(f, "Fetch error: {}", e),
// // State::Compile(e) => write!(f, "Compile error: {}", e), // State::Compile(e) => write!(f, "Compile error: {}", e),
// // State::Install(e) => write!(f, "Install error: {}", e), // State::Install(e) => write!(f, "Install error: {}", e),
// } }
// } }
// } }
impl<T> From<Result<T>> for State {
fn from(value: Result<T>) -> Self {
match value {
Ok(_) => State::Ok,
Err(e) => State::Err(e),
}
}
}
pub struct Summary { pub struct Summary {
// TODO: Go back to vectors to keep order? // TODO: Go back to vectors to keep order?
systems: Vec<Result<()>>, systems: Vec<State>,
} }
impl Display for Summary { impl Display for Summary {
@ -59,9 +65,8 @@ impl Display for Summary {
writeln!(f, "Summary:")?; writeln!(f, "Summary:")?;
for system in &self.systems { for system in &self.systems {
// TODO: also print version before/after, update time // TODO: also print version before/after, update time
// writeln!(f, "\t{}\t{}", system.0, system.1)?;
writeln!(f, "\t{:?}", system)?; writeln!(f, "\t{}", system)?;
} }
Ok(()) Ok(())
} }

View File

@ -2,7 +2,7 @@ use clap::Parser;
use system_updater::*; use system_updater::*;
fn main() { fn main() {
let mut opt = Opt::parse(); let opt = Opt::parse();
let summary = run(&opt); let summary = run(&opt);