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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use slog::{crit, error, Logger};
use thiserror::Error;

use mithril_common::entities::EpochError;
use mithril_common::StdError;

use crate::RunnerError;

/// RuntimeError
/// Error kinds tied to their faith in the state machine.
#[derive(Error, Debug)]
pub enum RuntimeError {
    /// KeepState error means the runtime will keep its state and try to cycle
    /// again.
    #[error("An error occurred, runtime state kept. message = '{message}'")]
    KeepState {
        /// Context error message
        message: String,

        /// Eventual previous error message
        #[source]
        nested_error: Option<StdError>,
    },
    /// Critical error means the runtime will exit and the software will return
    /// an error code.
    #[error("A critical error occurred, aborting runtime. message = '{message}'")]
    Critical {
        /// Context error message
        message: String,

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

impl RuntimeError {
    /// Easy matching Critical errors.
    pub fn is_critical(&self) -> bool {
        matches!(
            self,
            RuntimeError::Critical {
                message: _,
                nested_error: _
            }
        )
    }

    /// Write the error to the given logger.
    pub fn write_to_log(&self, logger: &Logger) {
        match self {
            Self::KeepState { nested_error, .. } => match nested_error {
                None => error!(logger, "{self}"),
                Some(err) => error!(logger, "{self}"; "nested_error" => ?err),
            },
            Self::Critical { nested_error, .. } => match nested_error {
                None => crit!(logger, "{self}"),
                Some(err) => crit!(logger, "{self}"; "nested_error" => ?err),
            },
        }
    }
}

impl From<RunnerError> for RuntimeError {
    fn from(value: RunnerError) -> Self {
        Self::KeepState {
            message: "runner failed".to_string(),
            nested_error: Some(value.into()),
        }
    }
}

impl From<EpochError> for RuntimeError {
    fn from(value: EpochError) -> Self {
        Self::KeepState {
            message: "Epoch offset conversion failed".to_string(),
            nested_error: Some(value.into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use anyhow::anyhow;
    use std::path::Path;

    use mithril_common::test_utils::TempDir;

    use crate::test_tools::TestLogger;

    use super::*;

    /// Separate function so the logger is dropped and flushed before the assertion.
    fn write_log(log_file: &Path, error: &RuntimeError) {
        let logger = TestLogger::file(log_file);
        error.write_to_log(&logger);
    }

    fn nested_error_debug_string(error: &RuntimeError) -> String {
        let error = match error {
            RuntimeError::KeepState { nested_error, .. } => nested_error,
            RuntimeError::Critical { nested_error, .. } => nested_error,
        };
        match error {
            None => String::new(),
            Some(err) => {
                format!("{err:?}")
            }
        }
    }

    #[test]
    fn log_critical_without_nested_error() {
        let log_file = TempDir::create("signer_runtime_error", "log_critical_without_nested_error")
            .join("file.log");

        let error = RuntimeError::Critical {
            message: "Critical error".to_string(),
            nested_error: None,
        };
        write_log(&log_file, &error);

        let log_content = std::fs::read_to_string(&log_file).unwrap();
        assert!(log_content.contains(&format!("{error}")));
        assert!(!log_content.contains("nested_error"));
    }

    #[test]
    fn log_critical_with_nested_error() {
        let log_file = TempDir::create("signer_runtime_error", "log_critical_with_nested_error")
            .join("file.log");

        let error = RuntimeError::Critical {
            message: "Critical error".to_string(),
            nested_error: Some(
                anyhow!("Another context error")
                    .context("Context error")
                    .context("Critical nested error"),
            ),
        };
        write_log(&log_file, &error);

        let log_content = std::fs::read_to_string(&log_file).unwrap();
        assert!(log_content.contains(&format!("{error}")));
        assert!(log_content.contains(&nested_error_debug_string(&error)));
    }

    #[test]
    fn log_keep_state_without_nested_error() {
        let log_file = TempDir::create(
            "signer_runtime_error",
            "log_keep_state_without_nested_error",
        )
        .join("file.log");

        let error = RuntimeError::KeepState {
            message: "KeepState error".to_string(),
            nested_error: None,
        };
        write_log(&log_file, &error);

        let log_content = std::fs::read_to_string(&log_file).unwrap();
        assert!(log_content.contains(&format!("{error}")));
        assert!(!log_content.contains("nested_error"));
    }

    #[test]
    fn log_keep_state_with_nested_error() {
        let log_file = TempDir::create("signer_runtime_error", "log_keep_state_with_nested_error")
            .join("file.log");

        let error = RuntimeError::KeepState {
            message: "KeepState error".to_string(),
            nested_error: Some(
                anyhow!("Another context error")
                    .context("Context error")
                    .context("KeepState nested error"),
            ),
        };
        write_log(&log_file, &error);

        let log_content = std::fs::read_to_string(&log_file).unwrap();
        assert!(log_content.contains(&format!("{error}")));
        assert!(log_content.contains(&nested_error_debug_string(&error)));
    }
}