Skip to main content

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/// This error can never be constructed when compiled without proteus.
5#[cfg(not(feature = "proteus"))]
6#[derive(Debug, thiserror::Error)]
7pub enum ProteusErrorKind {}
8
9/// Proteus produces these kinds of error
10#[cfg(feature = "proteus")]
11#[derive(Debug, thiserror::Error)]
12pub enum ProteusErrorKind {
13    /// Error when decoding CBOR and/or decrypting Proteus messages
14    #[error(transparent)]
15    ProteusDecodeError(#[from] proteus_wasm::DecodeError),
16    /// Error when encoding CBOR and/or decrypting Proteus messages
17    #[error(transparent)]
18    ProteusEncodeError(#[from] proteus_wasm::EncodeError),
19    /// Various internal Proteus errors
20    #[error(transparent)]
21    ProteusInternalError(#[from] proteus_wasm::error::ProteusError),
22    /// Error when there's a critical error within a proteus Session
23    #[error(transparent)]
24    ProteusSessionError(#[from] proteus_wasm::session::Error<core_crypto_keystore::CryptoKeystoreError>),
25    /// No session exists for the given session id
26    #[error("Couldn't find session {0}")]
27    SessionNotFound(String),
28}
29
30impl ProteusErrorKind {
31    #[cfg(feature = "proteus")]
32    fn proteus_error_code(&self) -> Option<proteus_traits::ProteusErrorKind> {
33        use proteus_traits::ProteusErrorCode as _;
34        let mut out = match self {
35            ProteusErrorKind::ProteusDecodeError(decode_error) => Some(decode_error.code()),
36            ProteusErrorKind::ProteusEncodeError(encode_error) => Some(encode_error.code()),
37            ProteusErrorKind::ProteusInternalError(proteus_error) => Some(proteus_error.code()),
38            ProteusErrorKind::ProteusSessionError(session_error) => Some(session_error.code()),
39            ProteusErrorKind::SessionNotFound(_) => Some(proteus_traits::ProteusErrorKind::SessionStateNotFoundForTag),
40        };
41        if out == Some(proteus_traits::ProteusErrorKind::None) {
42            out = None;
43        }
44        out
45    }
46    /// Returns the proteus error code
47    pub fn error_code(&self) -> Option<u16> {
48        #[cfg(feature = "proteus")]
49        {
50            self.proteus_error_code().map(|code| code as u16)
51        }
52
53        #[cfg(not(feature = "proteus"))]
54        {
55            None
56        }
57    }
58}