frequenz_microgrid/
error.rs1macro_rules! ErrorKind {
10 ($(
11 ($kind:ident, $ctor:ident)
12 ),* $(,)?) => {
13 #[derive(Debug, Clone, PartialEq)]
15 pub enum ErrorKind {
16 $(
17 $kind,
18 )*
19 }
20
21 impl std::fmt::Display for ErrorKind {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 match self {
24 $(
25 Self::$kind => write!(f, "{}", stringify!($kind)),
26 )*
27 }
28 }
29 }
30
31 impl Error {
33 $(
34 #[doc = concat!(
35 "Creates a new [`Error`] with the `",
36 stringify!($kind),
37 "` kind and the given description."
38 )]
39 pub(crate) fn $ctor(desc: impl Into<String>) -> crate::error::Error {
40 Self {
41 kind: ErrorKind::$kind,
42 desc: desc.into(),
43 }
44 }
45 )*
46
47 pub fn kind(&self) -> ErrorKind {
49 self.kind.clone()
50 }
51 }
52 };
53}
54
55ErrorKind!(
56 (ComponentGraphError, component_graph_error),
57 (ComponentDataError, component_data_error),
58 (ConnectionFailure, connection_failure),
59 (DroppedUnusedFormulas, dropped_unused_formulas),
60 (FormulaEngineError, formula_engine_error),
61 (InvalidComponent, invalid_component),
62 (InvalidConfig, invalid_config),
63 (Internal, internal),
64 (APIServerError, api_server_error),
65);
66
67#[derive(Debug, Clone, PartialEq)]
69pub struct Error {
70 kind: ErrorKind,
71 desc: String,
72}
73
74impl std::fmt::Display for Error {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 write!(f, "{}: {}", self.kind, self.desc)
77 }
78}
79
80impl std::error::Error for Error {}
81
82impl From<frequenz_microgrid_component_graph::Error> for Error {
83 fn from(error: frequenz_microgrid_component_graph::Error) -> Self {
84 Self::component_graph_error(error.to_string())
85 }
86}