Skip to main content

signstar_yubihsm2/
error.rs

1//! Error handling.
2
3use std::path::PathBuf;
4
5/// The error that may occur when using a YubiHSM2 device.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// A client operation failed.
9    #[error("YubiHSM client operation failed while {context}:\n{source}")]
10    Client {
11        /// The context in which the error occurred.
12        ///
13        /// This is meant to complete the sentence "YubiHSM client operation failed while ".
14        context: &'static str,
15
16        /// The source error.
17        source: yubihsm::client::Error,
18    },
19
20    /// A device operation failed.
21    #[error("YubiHSM device operation failed while {context}:\n{source}")]
22    Device {
23        /// The context in which the error occurred.
24        ///
25        /// This is meant to complete the sentence "YubiHSM device operation failed while ".
26        context: &'static str,
27
28        /// The source error.
29        source: yubihsm::device::Error,
30    },
31
32    /// A device operation failed.
33    #[error("YubiHSM domain operation failed while {context}:\n{source}")]
34    Domain {
35        /// The context in which the error occurred.
36        ///
37        /// This is meant to complete the sentence "YubiHSM domain operation failed while ".
38        context: &'static str,
39
40        /// The source error.
41        source: yubihsm::domain::Error,
42    },
43
44    /// A device operation failed.
45    #[error("Certificate generation failed while {context}:\n{source}")]
46    CertificateGeneration {
47        /// The context in which the error occurred.
48        ///
49        /// This is meant to complete the sentence "Certificate generation failed while ".
50        context: &'static str,
51
52        /// The source error.
53        source: signstar_crypto::Error,
54    },
55
56    /// An I/O error occurred for a file.
57    #[error("I/O error for file {path} while {context}: {source}")]
58    IoPath {
59        /// The path to the file for which the error occurred.
60        path: PathBuf,
61        /// The context in which the error occurs.
62        ///
63        /// This is meant to complete the sentence "I/O error for file {path} while ".
64        context: &'static str,
65        /// The error source.
66        source: std::io::Error,
67    },
68
69    /// An I/O error occurred for a file.
70    #[error("JSON serialization error while {context}: {source}")]
71    #[cfg(feature = "serde")]
72    Json {
73        /// The context in which the error occurs.
74        ///
75        /// This is meant to complete the sentence "JSON serialization error while ".
76        context: &'static str,
77        /// The error source.
78        source: serde_json::Error,
79    },
80
81    /// An I/O error occurred for a file.
82    #[error("Deserialization of the wrap file failed while {context}: {source}")]
83    InvalidWrap {
84        /// The context in which the error occurs.
85        ///
86        /// This is meant to complete the sentence "Deserialization of the wrap file failed while
87        /// ".
88        context: &'static str,
89        /// The error source.
90        source: yubihsm::wrap::Error,
91    },
92
93    /// A generic I/O error occurred.
94    #[error("I/O error occurred 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 occurred while ".
99        context: &'static str,
100        /// The error source.
101        source: std::io::Error,
102    },
103
104    /// A slice length error occurred.
105    #[error("Data had an incorrect length when {context}")]
106    IncorrectDataLength {
107        /// The context in which the error occurs.
108        ///
109        /// This is meant to complete the sentence "Data had an incorrect length when ".
110        context: &'static str,
111    },
112
113    /// A domain ID parse error occurred.
114    #[error("Domain ID cannot be parsed when {context}")]
115    DomainParseError {
116        /// The context in which the error occurs.
117        ///
118        /// This is meant to complete the sentence "Domain ID cannot be parsed when ".
119        context: &'static str,
120    },
121
122    /// Attempted to use functionality guarded by the "_yubihsm2-mockhsm" feature.
123    #[cfg(not(feature = "_yubihsm2-mockhsm"))]
124    #[error("The '_yubihsm2-mockhsm' feature is not available")]
125    MockHsmUnavailable,
126
127    /// A YubiHSM2 object error occurred.
128    #[error(transparent)]
129    Object(#[from] crate::object::Error),
130
131    /// A YubiHSM2 backup error occurred.
132    #[error(transparent)]
133    Backup(#[from] crate::backup::Error),
134
135    /// A YubiHSM2-crypto error occurred.
136    #[error(transparent)]
137    SignstarCrypto(#[from] signstar_crypto::Error),
138
139    /// A YubiHSM2 automation error occurred.
140    #[error(transparent)]
141    Automation(#[from] crate::automation::Error),
142}