Skip to main content

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