1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use mithril_common::StdError;
use thiserror::Error;

/// Error encountered or produced by the Runtime.
/// This enum represents the faith of the errors produced during the state
/// transitions.
#[derive(Error, Debug)]
pub enum RuntimeError {
    /// Errors that need the runtime to try again without changing its state.
    #[error("An error occured: {message}. This runtime cycle will be skipped.")]
    KeepState {
        /// error message
        message: String,

        /// Eventual caught error
        #[source]
        nested_error: Option<StdError>,
    },
    /// A Critical error means the Runtime stops and the software exits with an
    /// error code.
    #[error("Critical error:'{message}'.")]
    Critical {
        /// error message
        message: String,

        /// Eventual caught error
        #[source]
        nested_error: Option<StdError>,
    },
    /// An error that needs to re-initialize the state machine.
    #[error("An error occured: {message}. The state machine will be re-initialized.")]
    ReInit {
        /// error message
        message: String,

        /// Eventual caught error
        #[source]
        nested_error: Option<StdError>,
    },
}

impl RuntimeError {
    /// Create a new KeepState error
    pub fn keep_state(message: &str, error: Option<StdError>) -> Self {
        Self::KeepState {
            message: message.to_string(),
            nested_error: error,
        }
    }

    /// Create a new Critical error
    pub fn critical(message: &str, error: Option<StdError>) -> Self {
        Self::Critical {
            message: message.to_string(),
            nested_error: error,
        }
    }
}

impl From<StdError> for RuntimeError {
    fn from(value: StdError) -> Self {
        Self::KeepState {
            message: "Error caught, state preserved, will retry to cycle.".to_string(),
            nested_error: Some(value),
        }
    }
}