Better errors in summary
This commit is contained in:
parent
258d426ffd
commit
6769376ae2
@ -109,7 +109,7 @@ impl Updater {
|
||||
// self.systems.iter().collect();
|
||||
let mut systems: Vec<_> = vec![];
|
||||
for sys in &self.systems {
|
||||
systems.push(self.update(&sys, opt));
|
||||
systems.push(self.update(&sys, opt).into());
|
||||
}
|
||||
Summary { systems }
|
||||
}
|
||||
@ -145,10 +145,10 @@ impl System {
|
||||
let cmd = fetch.clone().prepare(opt);
|
||||
let exit_status = cmd
|
||||
.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() {
|
||||
return Err(MyError::new(
|
||||
ErrorKind::Fetch,
|
||||
MyErrorKind::Fetch,
|
||||
io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)),
|
||||
cmd.clone(),
|
||||
));
|
||||
@ -162,10 +162,10 @@ impl System {
|
||||
let cmd = compile.clone().prepare(opt);
|
||||
let exit_status = cmd
|
||||
.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() {
|
||||
return Err(MyError::new(
|
||||
ErrorKind::Fetch,
|
||||
MyErrorKind::Fetch,
|
||||
io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)),
|
||||
cmd.clone(),
|
||||
));
|
||||
@ -178,10 +178,10 @@ impl System {
|
||||
let cmd = self.install.clone().prepare(opt);
|
||||
let exit_status = cmd
|
||||
.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() {
|
||||
return Err(MyError::new(
|
||||
ErrorKind::Fetch,
|
||||
MyErrorKind::Fetch,
|
||||
io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)),
|
||||
cmd.clone(),
|
||||
));
|
||||
@ -201,7 +201,7 @@ impl Cmd {
|
||||
}
|
||||
|
||||
fn prepare(self, opt: &Opt) -> ActualCmd {
|
||||
let mut params = self.params;
|
||||
let params = self.params;
|
||||
|
||||
let mut env = self.env;
|
||||
if !env.contains_key("PATH") {
|
||||
@ -254,11 +254,14 @@ impl fmt::Display for ActualCmd {
|
||||
write!(f, " in {:?}", cdir)?;
|
||||
}
|
||||
|
||||
if !self.env.is_empty() {
|
||||
writeln!(f, " with the following environment variable:")?;
|
||||
writeln!(f, "{:#?}", self.env)
|
||||
} else {
|
||||
write!(f, " without any environment variable. ")
|
||||
}
|
||||
Ok(())
|
||||
|
||||
// // TODO: remove me (too verbose)
|
||||
// if !self.env.is_empty() {
|
||||
// writeln!(f, " with the following environment variable:")?;
|
||||
// writeln!(f, "{:#?}", self.env)
|
||||
// } else {
|
||||
// write!(f, " without any environment variable. ")
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,12 @@
|
||||
use crate::*;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use std::fmt;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io;
|
||||
use std::result;
|
||||
|
||||
pub type Result<T> = result::Result<T, MyError>;
|
||||
|
||||
/// An error that can occur in this crate.
|
||||
///
|
||||
@ -10,13 +14,13 @@ use std::io;
|
||||
#[derive(Debug)]
|
||||
pub struct MyError {
|
||||
source: io::Error,
|
||||
kind: ErrorKind,
|
||||
kind: MyErrorKind,
|
||||
cmd: ActualCmd,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum ErrorKind {
|
||||
pub enum MyErrorKind {
|
||||
Config,
|
||||
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?
|
||||
@ -24,12 +28,12 @@ pub enum ErrorKind {
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
/// Return the kind of this error.
|
||||
pub fn kind(&self) -> &ErrorKind {
|
||||
pub fn kind(&self) -> &MyErrorKind {
|
||||
&self.kind
|
||||
}
|
||||
}
|
||||
@ -41,28 +45,28 @@ impl Error for MyError {
|
||||
}
|
||||
|
||||
impl Display for MyError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self.kind {
|
||||
ErrorKind::Config => write!(
|
||||
MyErrorKind::Config => write!(
|
||||
f,
|
||||
"Could not read configuration file: {}",
|
||||
self.source().unwrap()
|
||||
),
|
||||
ErrorKind::Fetch => write!(
|
||||
MyErrorKind::Fetch => write!(
|
||||
f,
|
||||
"Could not fetch with command `{}`: {}",
|
||||
"Could not fetch with command {}: {}",
|
||||
self.cmd,
|
||||
self.source().unwrap()
|
||||
),
|
||||
ErrorKind::Compile => write!(
|
||||
MyErrorKind::Compile => write!(
|
||||
f,
|
||||
"Could not compile with command `{}`: {}",
|
||||
"Could not compile with command {}: {}",
|
||||
self.cmd,
|
||||
self.source().unwrap()
|
||||
),
|
||||
ErrorKind::Install => write!(
|
||||
MyErrorKind::Install => write!(
|
||||
f,
|
||||
"Could not install with command `{}`: {}",
|
||||
"Could not install with command {}: {}",
|
||||
self.cmd,
|
||||
self.source().unwrap()
|
||||
),
|
||||
|
49
src/lib.rs
49
src/lib.rs
@ -7,9 +7,6 @@ use errors::*;
|
||||
use clap::Parser;
|
||||
use std::fmt::Display;
|
||||
use std::path::PathBuf;
|
||||
use std::result;
|
||||
|
||||
pub type Result<T> = result::Result<T, MyError>;
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct Opt {
|
||||
@ -31,27 +28,36 @@ pub struct Opt {
|
||||
pub steps: Vec<String>,
|
||||
}
|
||||
|
||||
// enum State {
|
||||
// /// All steps asked were successful
|
||||
// Ok,
|
||||
// Err(MyError),
|
||||
// }
|
||||
enum State {
|
||||
/// All steps asked were successful
|
||||
Ok,
|
||||
Err(MyError),
|
||||
}
|
||||
|
||||
// impl Display for State {
|
||||
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
// match self {
|
||||
// State::Ok => write!(f, "Ok"),
|
||||
// State::Err(e) => write!(f, "Error: {}", e),
|
||||
// // State::Fetch(e) => write!(f, "Fetch error: {}", e),
|
||||
// // State::Compile(e) => write!(f, "Compile error: {}", e),
|
||||
// // State::Install(e) => write!(f, "Install error: {}", e),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
impl Display for State {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
State::Ok => write!(f, "Ok"),
|
||||
State::Err(e) => write!(f, "Error: {}", e),
|
||||
// State::Fetch(e) => write!(f, "Fetch error: {}", e),
|
||||
// State::Compile(e) => write!(f, "Compile 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 {
|
||||
// TODO: Go back to vectors to keep order?
|
||||
systems: Vec<Result<()>>,
|
||||
systems: Vec<State>,
|
||||
}
|
||||
|
||||
impl Display for Summary {
|
||||
@ -59,9 +65,8 @@ impl Display for Summary {
|
||||
writeln!(f, "Summary:")?;
|
||||
for system in &self.systems {
|
||||
// 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(())
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use clap::Parser;
|
||||
use system_updater::*;
|
||||
|
||||
fn main() {
|
||||
let mut opt = Opt::parse();
|
||||
let opt = Opt::parse();
|
||||
|
||||
let summary = run(&opt);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user