system-updater/src/errors.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2023-01-28 19:20:37 +01:00
use crate::*;
use std::error::Error;
use std::fmt::{Display, Formatter, Result};
use std::io;
2023-01-07 17:08:44 +01:00
/// An error that can occur in this crate.
///
/// Generally, this error corresponds to problems with underlying process.
#[derive(Debug)]
2023-01-28 19:20:37 +01:00
pub struct MyError {
source: io::Error,
2023-01-07 17:08:44 +01:00
kind: ErrorKind,
2023-01-28 19:20:37 +01:00
cmd: ActualCmd,
2023-01-07 17:08:44 +01:00
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorKind {
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?
Install, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error?
}
2023-01-28 19:20:37 +01:00
impl MyError {
pub(crate) fn new(kind: ErrorKind, source: io::Error, cmd: ActualCmd) -> MyError {
MyError { source, kind, cmd }
2023-01-07 17:08:44 +01:00
}
/// Return the kind of this error.
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
2023-01-28 19:20:37 +01:00
impl Error for MyError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.source)
2023-01-07 17:08:44 +01:00
}
}
2023-01-28 19:20:37 +01:00
impl Display for MyError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2023-01-07 17:08:44 +01:00
match self.kind {
ErrorKind::Config => write!(
f,
"Could not read configuration file: {}",
self.source().unwrap()
),
2023-01-28 19:20:37 +01:00
ErrorKind::Fetch => write!(
f,
"Could not fetch with command `{}`: {}",
self.cmd,
self.source().unwrap()
),
ErrorKind::Compile => write!(
f,
"Could not compile with command `{}`: {}",
self.cmd,
self.source().unwrap()
),
ErrorKind::Install => write!(
f,
"Could not install with command `{}`: {}",
self.cmd,
self.source().unwrap()
),
2023-01-07 17:08:44 +01:00
}
}
}