core_crypto/error/
proteus.rs

1/// A Proteus operation failed, but we captured some context about how it did so
2pub type ProteusError = super::wrapper::WrappedContextualError<ProteusErrorKind>;
3
4/// Proteus produces these kinds of error
5#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
6pub enum ProteusErrorKind {
7    #[cfg(feature = "proteus")]
8    #[error(transparent)]
9    /// Error when decoding CBOR and/or decrypting Proteus messages
10    ProteusDecodeError(#[from] proteus_wasm::DecodeError),
11    #[cfg(feature = "proteus")]
12    #[error(transparent)]
13    /// Error when encoding CBOR and/or decrypting Proteus messages
14    ProteusEncodeError(#[from] proteus_wasm::EncodeError),
15    #[cfg(feature = "proteus")]
16    #[error(transparent)]
17    /// Various internal Proteus errors
18    ProteusInternalError(#[from] proteus_wasm::error::ProteusError),
19    #[cfg(feature = "proteus")]
20    #[error(transparent)]
21    /// Error when there's a critical error within a proteus Session
22    ProteusSessionError(#[from] proteus_wasm::session::Error<core_crypto_keystore::CryptoKeystoreError>),
23    /// Common errors we generate
24    #[cfg(feature = "proteus")]
25    #[error("{0}")]
26    Leaf(#[from] crate::LeafError),
27}
28
29impl ProteusErrorKind {
30    #[cfg(feature = "proteus")]
31    fn proteus_error_code(&self) -> Option<proteus_traits::ProteusErrorKind> {
32        use proteus_traits::ProteusErrorCode as _;
33        let mut out = match self {
34            ProteusErrorKind::ProteusDecodeError(decode_error) => Some(decode_error.code()),
35            ProteusErrorKind::ProteusEncodeError(encode_error) => Some(encode_error.code()),
36            ProteusErrorKind::ProteusInternalError(proteus_error) => Some(proteus_error.code()),
37            ProteusErrorKind::ProteusSessionError(session_error) => Some(session_error.code()),
38            ProteusErrorKind::Leaf(crate::LeafError::ConversationNotFound(_)) => {
39                Some(proteus_traits::ProteusErrorKind::SessionStateNotFoundForTag)
40            }
41            ProteusErrorKind::Leaf(_) => None,
42        };
43        if out == Some(proteus_traits::ProteusErrorKind::None) {
44            out = None;
45        }
46        out
47    }
48    /// Returns the proteus error code
49    pub fn error_code(&self) -> Option<u16> {
50        cfg_if::cfg_if! {
51            if #[cfg(feature = "proteus")] {
52                self.proteus_error_code().map(|code| code as u16)
53            } else {
54                None
55            }
56        }
57    }
58}