jocker_lib/
error.rs

1use std::fmt::Display;
2
3/// Alias for a `Result` with the error type [`jocker::Error`].
4pub type Result<T> = std::result::Result<T, Error>;
5
6pub struct Error {
7    pub inner_error: InnerError,
8    pub debug_context: Vec<String>,
9}
10
11impl Error {
12    pub fn new(inner_error: InnerError) -> Self {
13        Self {
14            inner_error,
15            debug_context: vec![],
16        }
17    }
18
19    pub fn with_context<E: Into<Error>>(inner_error: InnerError) -> impl FnOnce(E) -> Self {
20        move |src| {
21            let err: Error = src.into();
22            err.add_context(inner_error.to_string())
23        }
24    }
25
26    pub fn add_context<T: Into<String>>(mut self, context: T) -> Self {
27        self.debug_context.push(context.into());
28        self
29    }
30}
31
32impl Display for Error {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(f, "{:?}", self.inner_error)?;
35        if !self.debug_context.is_empty() {
36            write!(f, " With context:")?;
37            for (idx, context) in self.debug_context.iter().enumerate() {
38                write!(f, "[{}] {}", idx + 1, context)?
39            }
40        }
41        Ok(())
42    }
43}
44
45impl std::fmt::Debug for Error {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(f, "JockerError: {}", self)
48    }
49}
50
51impl std::error::Error for Error {}
52
53impl<T: Into<InnerError>> From<T> for Error {
54    fn from(src: T) -> Self {
55        Error {
56            inner_error: src.into(),
57            debug_context: vec![],
58        }
59    }
60}
61
62#[derive(Debug, thiserror::Error)]
63pub enum InnerError {
64    #[error("cargo error")]
65    Cargo,
66    #[error("Env error")]
67    Env(String),
68    #[error("Filesystem error")]
69    Filesystem,
70    #[error("Lock error")]
71    Lock(String),
72    #[error("Parse error")]
73    Parse(String),
74    #[error("Process not found error")]
75    ProcessNotFound(Vec<String>),
76    #[error("ps error")]
77    Ps(String),
78    #[error("Recursion deepness too high")]
79    RecursionDeepnessTooHigh,
80    #[error("Recursion loop")]
81    RecursionLoop,
82    #[error("Stack not found error")]
83    StackNotFound(String),
84    #[error("Start stage error")]
85    Start(String),
86
87    #[error("UTF-8 error")]
88    FromUtf8Error(#[from] std::string::FromUtf8Error),
89    #[error("IO error")]
90    Io(#[from] std::io::Error),
91    #[error("ParseIntError error")]
92    ParseIntError(#[from] std::num::ParseIntError),
93    #[error("pueue error")]
94    Pueue(#[from] pueue_lib::Error),
95    #[error("redb commit error")]
96    RedbCommit(#[from] redb::CommitError),
97    #[error("redb database error")]
98    RedbDatabase(#[from] redb::DatabaseError),
99    #[error("redb storage error")]
100    RedbStorage(#[from] redb::StorageError),
101    #[error("redb table error")]
102    RedbTable(#[from] redb::TableError),
103    #[error("redb transaction error")]
104    RedbTransaction(#[from] redb::TransactionError),
105    #[error("redb upgrade error")]
106    RedbUpgrade(#[from] redb::UpgradeError),
107    #[error("Serde JSON error")]
108    SerdeJson(#[from] serde_json::Error),
109    #[error("Serde YAML error")]
110    SerdeYaml(#[from] serde_yml::Error),
111    #[error("SystemTime error")]
112    SystemTime(#[from] std::time::SystemTimeError),
113    #[error("TryFromInt error")]
114    TryFromInt(#[from] std::num::TryFromIntError),
115    #[error("URL error")]
116    Url(#[from] url::ParseError),
117    #[error("Var error")]
118    Var(#[from] std::env::VarError),
119}
120
121pub fn lock_error(e: impl Display) -> Error {
122    Error::new(InnerError::Lock(e.to_string()))
123}