forked from frequenz-floss/frequenz-microgrid-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
86 lines (76 loc) · 2.54 KB
/
error.rs
File metadata and controls
86 lines (76 loc) · 2.54 KB
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
// License: MIT
// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
//! This module defines the `Error` struct and the `ErrorKind` enum, which are
//! used to represent errors that can occur in the library.
/// A macro for defining the `ErrorKind` enum, the `Display` implementation for
/// it, and the constructors for the `Error` struct.
macro_rules! ErrorKind {
($(
($kind:ident, $ctor:ident)
),* $(,)?) => {
/// The kind of error that occurred.
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorKind {
$(
$kind,
)*
}
impl std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
$(
Self::$kind => write!(f, "{}", stringify!($kind)),
)*
}
}
}
/// Constructors for [`Error`].
impl Error {
$(
#[doc = concat!(
"Creates a new [`Error`] with the `",
stringify!($kind),
"` kind and the given description."
)]
pub(crate) fn $ctor(desc: impl Into<String>) -> crate::error::Error {
Self {
kind: ErrorKind::$kind,
desc: desc.into(),
}
}
)*
/// Returns the kind of error that occurred.
pub fn kind(&self) -> ErrorKind {
self.kind.clone()
}
}
};
}
ErrorKind!(
(ComponentGraphError, component_graph_error),
(ComponentDataError, component_data_error),
(ConnectionFailure, connection_failure),
(DroppedUnusedFormulas, dropped_unused_formulas),
(FormulaEngineError, formula_engine_error),
(InvalidComponent, invalid_component),
(InvalidConfig, invalid_config),
(Internal, internal),
(APIServerError, api_server_error),
);
/// An error that occurred in `frequenz_microgrid`.
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
kind: ErrorKind,
desc: String,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.kind, self.desc)
}
}
impl std::error::Error for Error {}
impl From<frequenz_microgrid_component_graph::Error> for Error {
fn from(error: frequenz_microgrid_component_graph::Error) -> Self {
Self::component_graph_error(error.to_string())
}
}