signstar_config/error.rs
1//! Common, top-level error type for all components of signstar-config.
2
3use std::{path::PathBuf, process::ExitStatus, string::FromUtf8Error};
4
5#[cfg(doc)]
6use crate::{admin_credentials::AdminCredentials, config::Config};
7
8/// An error that may occur when using Signstar config.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 /// An error specific to administrative secret handling.
12 #[error("Error with administrative secret handling:\n{0}")]
13 AdminSecretHandling(#[from] crate::admin_credentials::Error),
14
15 /// An error specific to Signstar config handling.
16 /// Applying permissions to a file or directory failed.
17 #[error("Unable to apply permissions from mode {mode} to {path}:\n{source}")]
18 ApplyPermissions {
19 /// The path to a file for which permissions can not be applied.
20 path: PathBuf,
21 /// The file mode that should be applied for `path`.
22 mode: u32,
23 /// The source error.
24 source: std::io::Error,
25 },
26
27 /// A [`change_user_run::Error`] occurred.
28 #[error(transparent)]
29 ChangeUserRun(#[from] change_user_run::Error),
30
31 /// The ownership of a path can not be changed.
32 #[error("Changing ownership of {path} to user {user} failed:\n{source}")]
33 Chown {
34 /// The path to a file for which ownership can not be changed.
35 path: PathBuf,
36 /// The system user that should be the new owner of `path`.
37 user: String,
38 /// The source error.
39 source: std::io::Error,
40 },
41
42 /// Unable to attach to stdin of a command.
43 #[error("Unable to attach to stdin of command \"{command}\"")]
44 CommandAttachToStdin {
45 /// The command for which attaching to stdin failed.
46 command: String,
47 },
48
49 /// A command exited unsuccessfully.
50 #[error("The command \"{command}\" could not be started in the background:\n{source}")]
51 CommandBackground {
52 /// The command that could not be started in the background.
53 command: String,
54 /// The source error.
55 source: std::io::Error,
56 },
57
58 /// A command could not be executed.
59 #[error("The command \"{command}\" could not be executed:\n{source}")]
60 CommandExec {
61 /// The command that could not be executed.
62 command: String,
63 /// The source error.
64 source: std::io::Error,
65 },
66
67 /// A command exited unsuccessfully.
68 #[error(
69 "The command \"{command}\" exited with non-zero status code \"{exit_status}\":\nstderr:\n{stderr}"
70 )]
71 CommandNonZero {
72 /// The command that exited with a non-zero exit code.
73 command: String,
74 /// The exit status of `command`.
75 exit_status: ExitStatus,
76 /// The stderr of `command`.
77 stderr: String,
78 },
79
80 /// Unable to write to stdin of a command.
81 #[error("Unable to write to stdin of command \"{command}\"")]
82 CommandWriteToStdin {
83 /// The command for which writing to stdin failed.
84 command: String,
85 /// The source error.
86 source: std::io::Error,
87 },
88
89 /// Configuration errors.
90 #[error("Signstar config error:\n{0}")]
91 Config(#[from] crate::config::Error),
92
93 /// An I/O error.
94 #[error("I/O error while {context}: {source}")]
95 Io {
96 /// The context in which the error occurs.
97 ///
98 /// This is meant to complete the sentence "I/O error while ".
99 context: String,
100
101 /// The source error.
102 source: std::io::Error,
103 },
104
105 /// An I/O error occurred for a file.
106 #[error("I/O error for file {path} while {context}: {source}")]
107 IoPath {
108 /// The path to the file for which the error occurred.
109 path: PathBuf,
110 /// The context in which the error occurs.
111 ///
112 /// This is meant to complete the sentence "I/O error for file {path} while ".
113 context: &'static str,
114 /// The error source.
115 source: std::io::Error,
116 },
117
118 /// The iteration of an [`AdminCredentials`] implementation and a [`Config`] do not match.
119 #[error(
120 "Iteration mismatch: Administrative credentials ({admin_creds}) vs. Signstar config ({signstar_config})"
121 )]
122 IterationMismatch {
123 /// The iteration of the [`AdminCredentials`] implementation.
124 admin_creds: u32,
125 /// The iteration of the [`Config`].
126 signstar_config: u32,
127 },
128
129 /// A NetHSM error.
130 #[cfg(feature = "nethsm")]
131 #[error("NetHSM error:\n{0}")]
132 NetHsm(#[from] nethsm::Error),
133
134 /// A NetHSM backend error
135 ///
136 /// This variant is used when actions for a NetHSM backend fail.
137 #[cfg(feature = "nethsm")]
138 #[error("NetHSM backend error:\n{0}")]
139 NetHsmBackend(#[from] crate::nethsm::Error),
140
141 /// Low-level administrative credentials handling in signstar-common failed.
142 #[error("Handling of administrative credentials failed:\n{0}")]
143 SignstarCommonAdminCreds(#[from] signstar_common::admin_credentials::Error),
144
145 /// A [`signstar_crypto::Error`] occurred.
146 #[error(transparent)]
147 SignstarCrypto(#[from] signstar_crypto::Error),
148
149 /// A [`signstar_yubihsm2::Error`] occurred.
150 #[cfg(feature = "yubihsm2")]
151 #[error(transparent)]
152 SignstarYubiHsm2(#[from] signstar_yubihsm2::Error),
153
154 /// An error occurred in the test module.
155 #[cfg(feature = "_test-helpers")]
156 #[error(transparent)]
157 Test(#[from] crate::test::Error),
158
159 /// Joining a thread returned an error.
160 #[error("Thread error while {context}")]
161 Thread {
162 /// The context in which the failed thread ran.
163 ///
164 /// Should complete the sentence "Thread error while ".
165 context: String,
166 },
167
168 /// TOML error while reading a file.
169 #[error("TOML read error for file {path} while {context}: {source}")]
170 TomlRead {
171 /// The path to a file that fails to read.
172 path: PathBuf,
173 /// The context in which the error occurs.
174 ///
175 /// This is meant to complete the sentence " error for file {path} while ".
176 context: &'static str,
177 /// The error source.
178 source: Box<toml::de::Error>,
179 },
180
181 /// TOML error while writing a file.
182 #[error("TOML write error for file {path} while {context}: {source}")]
183 TomlWrite {
184 /// The path to a file that fails to read.
185 path: PathBuf,
186 /// The context in which the error occurs.
187 ///
188 /// This is meant to complete the sentence " error for file {path} while ".
189 context: &'static str,
190 /// The error source.
191 source: toml::ser::Error,
192 },
193
194 /// An error occurred when using a Signstar config trait.
195 #[error(transparent)]
196 Traits(#[from] crate::config::TraitsError),
197
198 /// A UTF-8 error occurred when trying to convert a byte vector to a string.
199 #[error("Converting contents of {path} to string failed while {context}:\n{source}")]
200 Utf8String {
201 /// The path to a file for which conversion to UTF-8 string failed.
202 path: PathBuf,
203 /// The context in which the error occurred.
204 ///
205 /// Should complete the sentence "Converting contents of `path` to string failed while "
206 context: String,
207 /// The source error.
208 source: FromUtf8Error,
209 },
210
211 /// A utility function returned an error.
212 #[error("Utility function error: {0}")]
213 Utils(#[from] crate::utils::Error),
214
215 /// A garde validation error occurred.
216 #[error("Validation error while {context}: {source}")]
217 Validation {
218 /// The context in which the error occurred.
219 ///
220 /// This is meant to complete the sentence "Validation error while ".
221 context: String,
222
223 /// The error source.
224 source: garde::Report,
225 },
226
227 /// A NetHSM configuration object error occurred.
228 #[cfg(feature = "nethsm")]
229 #[error("NetHSM configuration object error: {0}")]
230 NetHsmConfig(#[from] crate::nethsm::NetHsmConfigError),
231
232 /// A YubiHSM2 configuration object error occurred.
233 #[cfg(feature = "yubihsm2")]
234 #[error("YubiHSM2 configuration object error: {0}")]
235 YubiHsm2Config(#[from] crate::yubihsm2::YubiHSM2ConfigError),
236
237 /// A YubiHSM2 backend error occurred.
238 #[cfg(feature = "yubihsm2")]
239 #[error("YubiHSM2 backend error: {0}")]
240 YubiHsm2Backend(#[from] crate::yubihsm2::Error),
241}