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(_) => None,
39        };
40        if out == Some(proteus_traits::ProteusErrorKind::None) {
41            out = None;
42        }
43        out
44    }
45    /// Returns the proteus error code
46    pub fn error_code(&self) -> Option<u16> {
47        cfg_if::cfg_if! {
48            if #[cfg(feature = "proteus")] {
49                self.proteus_error_code().map(|code| code as u16)
50            } else {
51                None
52            }
53        }
54    }
55}