mithril_aggregator/dependency_injection/
error.rs

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
//! Module dedicated to dependencies builder error management.

use std::{error::Error, fmt::Display};

use config::ConfigError;
use mithril_common::StdError;

/// Result with the [DependenciesBuilderError] error.
pub type Result<T> = std::result::Result<T, DependenciesBuilderError>;

/// Error that can occur during dependencies initialization process.
#[derive(Debug)]
pub enum DependenciesBuilderError {
    /// Unrecoverable system initialization failure
    Initialization {
        /// Error context message
        message: String,

        /// Eventual nested error
        error: Option<StdError>,
    },

    /// Configuration parameter missing for initialization.
    MissingConfiguration(String),

    /// The dependency has reached a state where dependencies are not consistent
    /// anymore. Shall be critical.
    InconsistentState(String),
}

impl Display for DependenciesBuilderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Initialization { message, error } => {
                if let Some(nested) = error {
                    write!(f, "Dependency initialization error: «{message}» with additional nested error: '{nested:?}'.")
                } else {
                    write!(f, "Dependency initialization error: «{message}».")
                }
            }
            Self::MissingConfiguration(field) => {
                write!(f, "Missing configuration setting '{field}'.")
            }
            Self::InconsistentState(message) => {
                write!(f, "Inconsistent dependency state: '{message}'.")
            }
        }
    }
}

impl Error for DependenciesBuilderError {}

impl From<StdError> for DependenciesBuilderError {
    fn from(value: StdError) -> Self {
        DependenciesBuilderError::Initialization {
            message: "subsystem error".to_string(),
            error: Some(value),
        }
    }
}

impl From<ConfigError> for DependenciesBuilderError {
    fn from(value: ConfigError) -> Self {
        Self::MissingConfiguration(format!("{value}"))
    }
}