core_crypto/error/
mod.rs

1mod keystore;
2mod leaf;
3mod mls;
4mod proteus;
5mod recursive;
6mod wrapper;
7
8pub use keystore::KeystoreError;
9pub use leaf::LeafError;
10pub use mls::{MlsError, MlsErrorKind};
11pub use proteus::{ProteusError, ProteusErrorKind};
12pub use recursive::{RecursiveError, ToRecursiveError};
13
14/// A module-specific [Result][core::result::Result] type with a default error variant.
15pub type Result<T, E = Error> = core::result::Result<T, E>;
16
17/// Errors produced by the root module group
18#[derive(Debug, thiserror::Error)]
19pub enum Error {
20    /// Invalid [crate::transaction_context::TransactionContext]. This context has been finished and can no longer be
21    /// used.
22    #[error("This transaction context has already been finished and can no longer be used.")]
23    InvalidTransactionContext,
24    /// The proteus client has been called but has not been initialized yet
25    #[error("Proteus client hasn't been initialized")]
26    ProteusNotInitialized,
27    /// Mls Transport Callbacks were not provided
28    #[error("The mls transport callbacks needed for CoreCrypto to operate were not set")]
29    MlsTransportNotProvided,
30    /// Any error that occurs during mls transport.
31    #[error("Error during mls transport: {0}")]
32    ErrorDuringMlsTransport(String),
33    /// This item requires a feature that the core-crypto library was built without
34    #[error("This item requires a feature that the core-crypto library was built without: {0}")]
35    FeatureDisabled(&'static str),
36    /// A key store operation failed
37    #[error(transparent)]
38    Keystore(#[from] KeystoreError),
39    /// Invalid history secret
40    #[error("Invalid history secret: {0}")]
41    InvalidHistorySecret(&'static str),
42    /// An external MLS operation failed
43    #[error(transparent)]
44    Mls(#[from] MlsError),
45    /// A Proteus operation failed
46    #[error(transparent)]
47    Proteus(#[from] ProteusError),
48    /// A crate-internal operation failed
49    #[error(transparent)]
50    Recursive(#[from] RecursiveError),
51}
52
53/// Produce the error message from the innermost wrapped error.
54///
55/// We produce arbitrarily nested errors which are very helpful
56/// at capturing relevant context, and very bad at surfacing the
57/// root error cause in a default `.to_string()` call.
58///
59/// This trait, automatically implemented for all standard errors,
60/// rectifies this problem.
61pub trait InnermostErrorMessage {
62    /// Produce the error message from the innermost wrapped error.
63    fn innermost_error_message(&self) -> String;
64}
65
66impl<E: std::error::Error> InnermostErrorMessage for E {
67    fn innermost_error_message(&self) -> String {
68        let mut err: &dyn std::error::Error = self;
69        while let Some(source) = err.source() {
70            err = source;
71        }
72        err.to_string()
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn can_unpack_wrapped_error() {
82        let inner = Error::InvalidTransactionContext;
83        let outer = RecursiveError::root("wrapping the inner for test purposes")(inner);
84        let message = outer.innermost_error_message();
85        assert_eq!(message, Error::InvalidTransactionContext.to_string());
86    }
87}