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