system-updater/src/errors.rs

76 lines
1.8 KiB
Rust
Raw Normal View History

2023-01-28 19:20:37 +01:00
use crate::*;
use std::error::Error;
2023-01-28 22:24:39 +01:00
use std::fmt;
use std::fmt::{Display, Formatter};
2023-01-28 19:20:37 +01:00
use std::io;
2023-01-28 22:24:39 +01:00
use std::result;
pub type Result<T> = result::Result<T, MyError>;
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-28 22:24:39 +01:00
kind: MyErrorKind,
2023-01-28 19:20:37 +01:00
cmd: ActualCmd,
2023-01-07 17:08:44 +01:00
}
#[derive(Debug)]
#[non_exhaustive]
2023-01-28 22:24:39 +01:00
pub enum MyErrorKind {
2023-01-07 17:08:44 +01:00
Config,
2023-06-28 22:46:56 +02:00
PreInstall,
2023-01-29 16:38:30 +01:00
Install,
PostInstall,
2023-01-07 17:08:44 +01:00
}
2023-01-28 19:20:37 +01:00
impl MyError {
2023-01-28 22:24:39 +01:00
pub(crate) fn new(kind: MyErrorKind, source: io::Error, cmd: ActualCmd) -> MyError {
2023-01-28 19:20:37 +01:00
MyError { source, kind, cmd }
2023-01-07 17:08:44 +01:00
}
/// Return the kind of this error.
2023-01-28 22:24:39 +01:00
pub fn kind(&self) -> &MyErrorKind {
2023-01-07 17:08:44 +01:00
&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 {
2023-01-28 22:24:39 +01:00
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2023-01-07 17:08:44 +01:00
match self.kind {
2023-01-28 22:24:39 +01:00
MyErrorKind::Config => write!(
2023-01-07 17:08:44 +01:00
f,
"Could not read configuration file: {}",
self.source().unwrap()
),
2023-06-28 22:46:56 +02:00
MyErrorKind::PreInstall => write!(
2023-01-28 19:20:37 +01:00
f,
2023-06-28 22:46:56 +02:00
"Could not do the pre_install with command {}: {}",
2023-01-28 19:20:37 +01:00
self.cmd,
self.source().unwrap()
),
2023-01-28 22:24:39 +01:00
MyErrorKind::Install => write!(
2023-01-28 19:20:37 +01:00
f,
2023-01-28 22:24:39 +01:00
"Could not install with command {}: {}",
2023-01-28 19:20:37 +01:00
self.cmd,
self.source().unwrap()
),
2023-01-29 16:38:30 +01:00
MyErrorKind::PostInstall => write!(
f,
"Could not do the post_install command {}: {}",
self.cmd,
self.source().unwrap()
),
2023-01-07 17:08:44 +01:00
}
}
}