1use std::fmt;
4
5use redup_config::Problems;
6
7pub type ModuleError = Box<dyn std::error::Error + Send + Sync + 'static>;
9
10#[derive(Debug)]
12pub enum Error {
13 Config {
15 owner: String,
17 problems: Problems,
19 },
20 Create {
22 module: &'static str,
24 source: ModuleError,
26 },
27 Start {
29 module: &'static str,
31 source: ModuleError,
33 },
34 DuplicateModule {
36 name: &'static str,
38 },
39 Off {
41 module: &'static str,
43 },
44 Dependency {
46 module: &'static str,
48 name: String,
50 reason: &'static str,
52 },
53 Bind {
55 address: String,
57 source: std::io::Error,
59 },
60 State {
62 action: &'static str,
64 },
65 Grpc {
67 source: ModuleError,
69 },
70 Telemetry {
72 action: &'static str,
74 source: ModuleError,
76 },
77}
78
79impl fmt::Display for Error {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 match self {
82 Self::Config { owner, problems } => {
83 write!(f, "the configuration of {owner} is rejected: {problems}")
84 }
85 Self::Create { module, source } => {
86 write!(f, "module '{module}' failed to be created: {source}")
87 }
88 Self::Start { module, source } => {
89 write!(f, "module '{module}' failed to start: {source}")
90 }
91 Self::DuplicateModule { name } => {
92 write!(f, "module name '{name}' is declared twice in the instance")
93 }
94 Self::Off { module } => write!(
95 f,
96 "module '{module}' is off: its configuration section is absent or null, \
97 but another module requires it"
98 ),
99 Self::Dependency {
100 module,
101 name,
102 reason,
103 } => {
104 write!(f, "dependency '{module}/{name}' {reason}")
105 }
106 Self::Bind { address, source } => {
107 write!(f, "listener '{address}' failed to start: {source}")
108 }
109 Self::State { action } => {
110 write!(f, "the instance cannot {action} in its current state")
111 }
112 Self::Grpc { source } => write!(f, "the gRPC server failed to start: {source}"),
113 Self::Telemetry { action, source } => {
114 write!(f, "telemetry failed to {action}: {source}")
115 }
116 }
117 }
118}
119
120impl std::error::Error for Error {
121 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
122 match self {
123 Self::Config { problems, .. } => Some(problems),
124 Self::Create { source, .. }
125 | Self::Start { source, .. }
126 | Self::Grpc { source }
127 | Self::Telemetry { source, .. } => Some(source.as_ref()),
128 Self::Bind { source, .. } => Some(source),
129 _ => None,
130 }
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::Error;
137
138 #[test]
139 fn off_names_the_module() {
140 let error = Error::Off { module: "Storage" };
141 assert_eq!(
142 error.to_string(),
143 "module 'Storage' is off: its configuration section is absent or null, but another \
144 module requires it"
145 );
146 }
147}