Skip to main content

core_crypto/error/
mod.rs

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