core_crypto/error/
proteus.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17/// A Proteus operation failed, but we captured some context about how it did so
18pub type ProteusError = super::wrapper::WrappedContextualError<ProteusErrorKind>;
19
20/// Proteus produces these kinds of error
21#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
22pub enum ProteusErrorKind {
23    #[cfg(feature = "proteus")]
24    #[error(transparent)]
25    /// Error when decoding CBOR and/or decrypting Proteus messages
26    ProteusDecodeError(#[from] proteus_wasm::DecodeError),
27    #[cfg(feature = "proteus")]
28    #[error(transparent)]
29    /// Error when encoding CBOR and/or decrypting Proteus messages
30    ProteusEncodeError(#[from] proteus_wasm::EncodeError),
31    #[cfg(feature = "proteus")]
32    #[error(transparent)]
33    /// Various internal Proteus errors
34    ProteusInternalError(#[from] proteus_wasm::error::ProteusError),
35    #[cfg(feature = "proteus")]
36    #[error(transparent)]
37    /// Error when there's a critical error within a proteus Session
38    ProteusSessionError(#[from] proteus_wasm::session::Error<core_crypto_keystore::CryptoKeystoreError>),
39    /// Common errors we generate
40    #[cfg(feature = "proteus")]
41    #[error("{0}")]
42    Leaf(#[from] crate::LeafError),
43}
44
45impl ProteusErrorKind {
46    #[cfg(feature = "proteus")]
47    fn proteus_error_code(&self) -> Option<proteus_traits::ProteusErrorKind> {
48        use proteus_traits::ProteusErrorCode as _;
49        let mut out = match self {
50            ProteusErrorKind::ProteusDecodeError(decode_error) => Some(decode_error.code()),
51            ProteusErrorKind::ProteusEncodeError(encode_error) => Some(encode_error.code()),
52            ProteusErrorKind::ProteusInternalError(proteus_error) => Some(proteus_error.code()),
53            ProteusErrorKind::ProteusSessionError(session_error) => Some(session_error.code()),
54            ProteusErrorKind::Leaf(_) => None,
55        };
56        if out == Some(proteus_traits::ProteusErrorKind::None) {
57            out = None;
58        }
59        out
60    }
61    /// Returns the proteus error code
62    pub fn error_code(&self) -> Option<u16> {
63        cfg_if::cfg_if! {
64            if #[cfg(feature = "proteus")] {
65                self.proteus_error_code().map(|code| code as u16)
66            } else {
67                None
68            }
69        }
70    }
71}