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