1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
34// 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.
89// 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.
1314// 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/.
1617/// A Proteus operation failed, but we captured some context about how it did so
18pub type ProteusError = super::wrapper::WrappedContextualError<ProteusErrorKind>;
1920/// 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
26ProteusDecodeError(#[from] proteus_wasm::DecodeError),
27#[cfg(feature = "proteus")]
28 #[error(transparent)]
29/// Error when encoding CBOR and/or decrypting Proteus messages
30ProteusEncodeError(#[from] proteus_wasm::EncodeError),
31#[cfg(feature = "proteus")]
32 #[error(transparent)]
33/// Various internal Proteus errors
34ProteusInternalError(#[from] proteus_wasm::error::ProteusError),
35#[cfg(feature = "proteus")]
36 #[error(transparent)]
37/// Error when there's a critical error within a proteus Session
38ProteusSessionError(#[from] proteus_wasm::session::Error<core_crypto_keystore::CryptoKeystoreError>),
39/// Common errors we generate
40#[cfg(feature = "proteus")]
41 #[error("{0}")]
42Leaf(#[from] crate::LeafError),
43}
4445impl ProteusErrorKind {
46#[cfg(feature = "proteus")]
47fn proteus_error_code(&self) -> Option<proteus_traits::ProteusErrorKind> {
48use proteus_traits::ProteusErrorCode as _;
49let 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 };
56if out == Some(proteus_traits::ProteusErrorKind::None) {
57 out = None;
58 }
59 out
60 }
61/// Returns the proteus error code
62pub fn error_code(&self) -> Option<u16> {
63cfg_if::cfg_if! {
64if #[cfg(feature = "proteus")] {
65self.proteus_error_code().map(|code| code as u16)
66 } else {
67None
68}
69 }
70 }
71}