core_crypto/error/recursive.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
// Wire
// Copyright (C) 2022 Wire Swiss GmbH
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
/// These errors wrap each of the module-specific errors in CoreCrypto.
///
/// The goal here is to reduce the need to redeclare each of these error
/// types as an individual variant of a module-specific error type.
#[derive(Debug)]
pub enum RecursiveError {
/// Wrap a [crate::Error] for recursion.
Root {
/// What was happening in the caller
context: &'static str,
/// What happened
source: Box<crate::Error>,
},
/// Wrap a [crate::e2e_identity::Error] for recursion.
E2e {
/// What was happening in the caller
context: &'static str,
/// What happened
source: Box<crate::e2e_identity::Error>,
},
/// Wrap a [crate::mls::Error] for recursion.
Mls {
/// What was happening in the caller
context: &'static str,
/// What happened
source: Box<crate::mls::Error>,
},
/// Wrap a [crate::mls::client::Error] for recursion.
MlsClient {
/// What was happening in the caller
context: &'static str,
/// What happened
source: Box<crate::mls::client::Error>,
},
/// Wrap a [crate::mls::conversation::Error] for recursion.
MlsConversation {
/// What was happening in the caller
context: &'static str,
/// What happened
source: Box<crate::mls::conversation::Error>,
},
/// Wrap a [crate::mls::credential::Error] for recursion.
MlsCredential {
/// What was happening in the caller
context: &'static str,
/// What happened
source: Box<crate::mls::credential::Error>,
},
/// Wrap a [crate::test_utils::TestError] for recursion.
#[cfg(test)]
Test(Box<crate::test_utils::TestError>),
}
impl RecursiveError {
/// Convert a [crate::Error] into a [RecursiveError], with context
pub fn root<E: Into<crate::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
move |into_source| Self::Root {
context,
source: Box::new(into_source.into()),
}
}
/// Convert a [crate::e2e_identity::Error] into a [RecursiveError], with context
pub fn e2e_identity<E: Into<crate::e2e_identity::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
move |into_source| Self::E2e {
context,
source: Box::new(into_source.into()),
}
}
/// Convert a [crate::mls::Error] into a [RecursiveError], with context
pub fn mls<E: Into<crate::mls::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
move |into_source| Self::Mls {
context,
source: Box::new(into_source.into()),
}
}
/// Convert a [crate::mls::client::Error] into a [RecursiveError], with context
pub fn mls_client<E: Into<crate::mls::client::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
move |into_source| Self::MlsClient {
context,
source: Box::new(into_source.into()),
}
}
/// Convert a [crate::mls::conversation::Error] into a [RecursiveError], with context
pub fn mls_conversation<E: Into<crate::mls::conversation::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
move |into_source| Self::MlsConversation {
context,
source: Box::new(into_source.into()),
}
}
/// Convert a [crate::mls::credential::Error] into a [RecursiveError], with context
pub fn mls_credential<E: Into<crate::mls::credential::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
move |into_source| Self::MlsCredential {
context,
source: Box::new(into_source.into()),
}
}
#[cfg(test)]
pub(crate) fn test<E: Into<crate::test_utils::TestError>>() -> impl FnOnce(E) -> Self {
move |into_source| Self::Test(Box::new(into_source.into()))
}
}
impl std::fmt::Display for RecursiveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
#[cfg(test)]
use std::ops::Deref;
let context = match self {
RecursiveError::Root { context, .. } => context,
RecursiveError::E2e { context, .. } => context,
RecursiveError::Mls { context, .. } => context,
RecursiveError::MlsClient { context, .. } => context,
RecursiveError::MlsConversation { context, .. } => context,
RecursiveError::MlsCredential { context, .. } => context,
#[cfg(test)]
RecursiveError::Test(e) => return e.deref().fmt(f),
};
write!(f, "{}", context)
}
}
impl std::error::Error for RecursiveError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RecursiveError::Root { source, .. } => Some(source.as_ref()),
RecursiveError::E2e { source, .. } => Some(source.as_ref()),
RecursiveError::Mls { source, .. } => Some(source.as_ref()),
RecursiveError::MlsClient { source, .. } => Some(source.as_ref()),
RecursiveError::MlsConversation { source, .. } => Some(source.as_ref()),
RecursiveError::MlsCredential { source, .. } => Some(source.as_ref()),
#[cfg(test)]
RecursiveError::Test(source) => Some(source.as_ref()),
}
}
}
/// Like [`Into`], but different, because we don't actually want to implement `Into` for our subordinate error types.
///
/// By forcing ourselves to map errors everywhere in order for question mark operators to work, we ensure that
pub trait ToRecursiveError {
/// Construct a recursive error given the current context
fn construct_recursive(self, context: &'static str) -> RecursiveError;
}
macro_rules! impl_to_recursive_error_for {
($($for:path => $variant:ident),+ $(,)?) => {
$(
impl ToRecursiveError for $for {
fn construct_recursive(self, context: &'static str) -> RecursiveError {
RecursiveError::$variant {
context,
source: Box::new(self),
}
}
}
)+
};
}
impl_to_recursive_error_for!(
crate::Error => Root,
crate::e2e_identity::Error => E2e,
crate::mls::Error => Mls,
crate::mls::client::Error => MlsClient,
crate::mls::conversation::Error => MlsConversation,
crate::mls::credential::Error => MlsCredential,
);