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 /// An error occurred in the test module.
150 #[cfg(feature = "_test-helpers")]
151 #[error(transparent)]
152 Test(#[from] crate::test::Error),
153
154 /// Joining a thread returned an error.
155 #[error("Thread error while {context}")]
156 Thread {
157 /// The context in which the failed thread ran.
158 ///
159 /// Should complete the sentence "Thread error while ".
160 context: String,
161 },
162
163 /// TOML error while reading a file.
164 #[error("TOML read error for file {path} while {context}: {source}")]
165 TomlRead {
166 /// The path to a file that fails to read.
167 path: PathBuf,
168 /// The context in which the error occurs.
169 ///
170 /// This is meant to complete the sentence " error for file {path} while ".
171 context: &'static str,
172 /// The error source.
173 source: Box<toml::de::Error>,
174 },
175
176 /// TOML error while writing a file.
177 #[error("TOML write error for file {path} while {context}: {source}")]
178 TomlWrite {
179 /// The path to a file that fails to read.
180 path: PathBuf,
181 /// The context in which the error occurs.
182 ///
183 /// This is meant to complete the sentence " error for file {path} while ".
184 context: &'static str,
185 /// The error source.
186 source: toml::ser::Error,
187 },
188
189 /// An error occurred when using a Signstar config trait.
190 #[error(transparent)]
191 Traits(#[from] crate::config::TraitsError),
192
193 /// A UTF-8 error occurred when trying to convert a byte vector to a string.
194 #[error("Converting contents of {path} to string failed while {context}:\n{source}")]
195 Utf8String {
196 /// The path to a file for which conversion to UTF-8 string failed.
197 path: PathBuf,
198 /// The context in which the error occurred.
199 ///
200 /// Should complete the sentence "Converting contents of `path` to string failed while "
201 context: String,
202 /// The source error.
203 source: FromUtf8Error,
204 },
205
206 /// A utility function returned an error.
207 #[error("Utility function error: {0}")]
208 Utils(#[from] crate::utils::Error),
209
210 /// A garde validation error occurred.
211 #[error("Validation error while {context}: {source}")]
212 Validation {
213 /// The context in which the error occurred.
214 ///
215 /// This is meant to complete the sentence "Validation error while ".
216 context: String,
217
218 /// The error source.
219 source: garde::Report,
220 },
221
222 /// A NetHSM configuration object error occurred.
223 #[cfg(feature = "nethsm")]
224 #[error("NetHSM configuration object error: {0}")]
225 NetHsmConfig(#[from] crate::nethsm::NetHsmConfigError),
226
227 /// A YubiHSM2 configuration object error occurred.
228 #[cfg(feature = "yubihsm2")]
229 #[error("YubiHSM2 configuration object error: {0}")]
230 YubiHsm2Config(#[from] crate::yubihsm2::YubiHSM2ConfigError),
231}