mithril_aggregator/dependency_injection/
error.rs

1//! Module dedicated to dependencies builder error management.
2
3use std::{error::Error, fmt::Display};
4
5use config::ConfigError;
6use mithril_common::StdError;
7
8/// Result with the [DependenciesBuilderError] error.
9pub type Result<T> = std::result::Result<T, DependenciesBuilderError>;
10
11/// Error that can occur during dependencies initialization process.
12#[derive(Debug)]
13pub enum DependenciesBuilderError {
14    /// Unrecoverable system initialization failure
15    Initialization {
16        /// Error context message
17        message: String,
18
19        /// Eventual nested error
20        error: Option<StdError>,
21    },
22
23    /// Configuration parameter missing for initialization.
24    MissingConfiguration(String),
25
26    /// The dependency has reached a state where dependencies are not consistent
27    /// anymore. Shall be critical.
28    InconsistentState(String),
29}
30
31impl Display for DependenciesBuilderError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            Self::Initialization { message, error } => {
35                if let Some(nested) = error {
36                    write!(
37                        f,
38                        "Dependency initialization error: «{message}» with additional nested error: '{nested:?}'."
39                    )
40                } else {
41                    write!(f, "Dependency initialization error: «{message}».")
42                }
43            }
44            Self::MissingConfiguration(field) => {
45                write!(f, "Missing configuration setting '{field}'.")
46            }
47            Self::InconsistentState(message) => {
48                write!(f, "Inconsistent dependency state: '{message}'.")
49            }
50        }
51    }
52}
53
54impl Error for DependenciesBuilderError {}
55
56impl From<StdError> for DependenciesBuilderError {
57    fn from(value: StdError) -> Self {
58        DependenciesBuilderError::Initialization {
59            message: "subsystem error".to_string(),
60            error: Some(value),
61        }
62    }
63}
64
65impl From<ConfigError> for DependenciesBuilderError {
66    fn from(value: ConfigError) -> Self {
67        Self::MissingConfiguration(format!("{value}"))
68    }
69}