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("Certificate generation failed while {context}:\n{source}")]
34    CertificateGeneration {
35        /// The context in which the error occurred.
36        ///
37        /// This is meant to complete the sentence "Certificate generation failed while ".
38        context: &'static str,
39
40        /// The source error.
41        source: signstar_crypto::signer::error::Error,
42    },
43
44    /// An I/O error occurred for a file.
45    #[error("I/O error for file {path} while {context}: {source}")]
46    IoPath {
47        /// The path to the file for which the error occurred.
48        path: PathBuf,
49        /// The context in which the error occurs.
50        ///
51        /// This is meant to complete the sentence "I/O error for file {path} while ".
52        context: &'static str,
53        /// The error source.
54        source: std::io::Error,
55    },
56
57    /// An I/O error occurred for a file.
58    #[error("JSON serialization error while {context}: {source}")]
59    #[cfg(feature = "serde")]
60    Json {
61        /// The context in which the error occurs.
62        ///
63        /// This is meant to complete the sentence "JSON serialization error while ".
64        context: &'static str,
65        /// The error source.
66        source: serde_json::Error,
67    },
68
69    /// An I/O error occurred for a file.
70    #[error("Deserialization of the wrap file failed while {context}: {source}")]
71    InvalidWrap {
72        /// The context in which the error occurs.
73        ///
74        /// This is meant to complete the sentence "Deserialization of the wrap file failed while
75        /// ".
76        context: &'static str,
77        /// The error source.
78        source: yubihsm::wrap::Error,
79    },
80
81    /// A generic I/O error occurred.
82    #[error("I/O error occurred while {context}: {source}")]
83    Io {
84        /// The context in which the error occurs.
85        ///
86        /// This is meant to complete the sentence "I/O error occurred while ".
87        context: &'static str,
88        /// The error source.
89        source: std::io::Error,
90    },
91
92    /// Attempted to use functionality guarded by the "_yubihsm2-mockhsm" feature.
93    #[cfg(not(feature = "_yubihsm2-mockhsm"))]
94    #[error("The '_yubihsm2-mockhsm' feature is not available")]
95    MockHsmUnavailable,
96
97    /// A YubiHSM2 object error occurred.
98    #[error(transparent)]
99    Object(#[from] crate::object::Error),
100
101    /// A YubiHSM2 backup error occurred.
102    #[error(transparent)]
103    Backup(#[from] crate::backup::Error),
104}