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 Rest {
72 source: ModuleError,
74 },
75 Surface {
77 what: &'static str,
79 reason: String,
81 },
82 Telemetry {
84 action: &'static str,
86 source: ModuleError,
88 },
89}
90
91impl fmt::Display for Error {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 match self {
94 Self::Config { owner, problems } => {
95 write!(f, "the configuration of {owner} is rejected: {problems}")
96 }
97 Self::Create { module, source } => {
98 write!(f, "module '{module}' failed to be created: {source}")
99 }
100 Self::Start { module, source } => {
101 write!(f, "module '{module}' failed to start: {source}")
102 }
103 Self::DuplicateModule { name } => {
104 write!(f, "module name '{name}' is declared twice in the instance")
105 }
106 Self::Off { module } => write!(
107 f,
108 "module '{module}' is off: its configuration section is absent or null, \
109 but another module requires it"
110 ),
111 Self::Dependency {
112 module,
113 name,
114 reason,
115 } => {
116 write!(f, "dependency '{module}/{name}' {reason}")
117 }
118 Self::Bind { address, source } => {
119 write!(f, "listener '{address}' failed to start: {source}")
120 }
121 Self::State { action } => {
122 write!(f, "the instance cannot {action} in its current state")
123 }
124 Self::Grpc { source } => write!(f, "the gRPC server failed to start: {source}"),
125 Self::Rest { source } => write!(f, "the REST surface failed to start: {source}"),
126 Self::Surface { what, reason } => {
127 write!(f, "the HTTP surface '{what}' failed to start: {reason}")
128 }
129 Self::Telemetry { action, source } => {
130 write!(f, "telemetry failed to {action}: {source}")
131 }
132 }
133 }
134}
135
136impl std::error::Error for Error {
137 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
138 match self {
139 Self::Config { problems, .. } => Some(problems),
140 Self::Create { source, .. }
141 | Self::Start { source, .. }
142 | Self::Grpc { source }
143 | Self::Telemetry { source, .. } => Some(source.as_ref()),
144 Self::Bind { source, .. } => Some(source),
145 _ => None,
146 }
147 }
148}
149
150#[cfg(test)]
151mod tests {
152 use super::Error;
153
154 #[test]
155 fn off_names_the_module() {
156 let error = Error::Off { module: "Storage" };
157 assert_eq!(
158 error.to_string(),
159 "module 'Storage' is off: its configuration section is absent or null, but another \
160 module requires it"
161 );
162 }
163}