core_crypto_ffi/error/
proteus.rs

1#[derive(Debug, thiserror::Error)]
2#[cfg_attr(target_family = "wasm", derive(strum::AsRefStr))]
3#[cfg_attr(not(target_family = "wasm"), derive(uniffi::Error))]
4pub enum ProteusError {
5    #[error("The requested session was not found")]
6    SessionNotFound,
7    #[error("We already decrypted this message once")]
8    DuplicateMessage,
9    #[error("The remote identity has changed")]
10    RemoteIdentityChanged,
11    #[error("Another Proteus error occurred but the details are probably irrelevant to clients ({0})")]
12    Other(u16),
13}
14
15impl ProteusError {
16    pub fn from_error_code(code: impl Into<Option<u16>>) -> Option<Self> {
17        let code = code.into()?;
18        if code == 0 {
19            return None;
20        }
21
22        match code {
23            102 => Self::SessionNotFound,
24            204 => Self::RemoteIdentityChanged,
25            209 => Self::DuplicateMessage,
26            _ => Self::Other(code),
27        }
28        .into()
29    }
30
31    pub fn error_code(&self) -> u16 {
32        match self {
33            Self::SessionNotFound => 102,
34            Self::RemoteIdentityChanged => 204,
35            Self::DuplicateMessage => 209,
36            Self::Other(code) => *code,
37        }
38    }
39}
40
41impl From<core_crypto::ProteusError> for ProteusError {
42    fn from(value: core_crypto::ProteusError) -> Self {
43        type SessionError = proteus_wasm::session::Error<core_crypto_keystore::CryptoKeystoreError>;
44        match value.source {
45            core_crypto::ProteusErrorKind::ProteusSessionError(SessionError::InternalError(
46                proteus_wasm::internal::types::InternalError::NoSessionForTag,
47            )) => Self::SessionNotFound,
48            core_crypto::ProteusErrorKind::ProteusSessionError(SessionError::DuplicateMessage) => {
49                Self::DuplicateMessage
50            }
51            core_crypto::ProteusErrorKind::ProteusSessionError(SessionError::RemoteIdentityChanged) => {
52                Self::RemoteIdentityChanged
53            }
54            _ => Self::Other(value.source.error_code().unwrap_or_default()),
55        }
56    }
57}