Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: errors per module [WPB-14354] #791

Merged
merged 31 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4d5244b
build(mls/client): byte literals exist so that humans do not have to …
coriolinus Nov 27, 2024
d548298
refactor!(mls): simplify `get_conversation` api
coriolinus Nov 27, 2024
807a9d1
refactor!(e2e_identity): module uses a module-specific error type
coriolinus Nov 27, 2024
c4a2b1c
refactor!(mls/client): client module uses a module-internal error type
coriolinus Nov 28, 2024
a782d53
refactor!(mls/conversation): conversation module uses a module-intern…
coriolinus Nov 29, 2024
2361365
refactor!(mls/credential): credential module uses a module-internal e…
coriolinus Nov 29, 2024
37730a7
refactor!(mls): mls root module uses a module-internal error type
coriolinus Nov 29, 2024
3aa7e84
refactor!(core-crypto): root module uses module-internal error type
coriolinus Dec 3, 2024
5d0541a
refactor!(errors): reexport each from module root
coriolinus Dec 4, 2024
b0dbc11
refactor(errors): extract `LeafError` to consolidate error leaves fro…
coriolinus Dec 4, 2024
7dd921f
refactor!(errors): centralize recursive error definitions to simplify
coriolinus Dec 4, 2024
a612ad8
refactor!(errors): centralize `KeystoreError`
coriolinus Dec 4, 2024
373ac0a
refactor(errors): better organize top-level errors
coriolinus Dec 5, 2024
4bd521a
fix(errors): make core-crypto compile again for `wasm32-unknown-unknown`
coriolinus Dec 5, 2024
1a083e3
refactor(errors): move `error::Error::ConversationNotFound` into `Lea…
coriolinus Dec 5, 2024
b9a9190
refactor(e2e_identity/errors): audit error type and simplify where po…
coriolinus Dec 5, 2024
cb7821c
refactor(mls/client/errors): audit error type and simplify where poss…
coriolinus Dec 5, 2024
17e4e89
refactor(mls/conversation/errors): audit error type and simplify wher…
coriolinus Dec 5, 2024
5b68c75
partial(errors): get some core-crypto test cases passing again
coriolinus Dec 6, 2024
65f672b
fix(errors): fix remaining failing test cases
coriolinus Dec 9, 2024
81dd49a
refactor(crypto-ffi): `core-crypto-ffi` builds given new errors
coriolinus Dec 12, 2024
7fdeeb4
fix: fix `check` ci action
coriolinus Dec 13, 2024
d0db260
fix: make leaf node validation tests pass
coriolinus Dec 13, 2024
15cbf13
build: reduce visibility of unreachable pub items
coriolinus Dec 13, 2024
e1d6dc5
fix: cause doctests to build/pass
coriolinus Dec 13, 2024
eab63e3
fix: ensure everything still builds in arbitray feature combinations
coriolinus Dec 13, 2024
4181deb
fix(kotlin): cause kotlin to build again
coriolinus Dec 13, 2024
a61739b
chore: rm legacy error type
coriolinus Dec 18, 2024
c84da0b
chore: typos and vocab fixes
coriolinus Dec 18, 2024
8cd3806
fixup: `innermost_source_matches` can handle dereferencing boxed errors
coriolinus Dec 18, 2024
e93cee6
chore: remove error variants already removed by `c5482e5`
coriolinus Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ sealed class CoreCryptoException: kotlin.Exception() {
class ClientException(
override val message: String
) : CoreCryptoException()

class Other(override val message: String) : CoreCryptoException()
}

fun com.wire.crypto.uniffi.CoreCryptoException.lift() =
Expand All @@ -135,6 +137,7 @@ fun com.wire.crypto.uniffi.CoreCryptoException.lift() =
is com.wire.crypto.uniffi.CoreCryptoException.E2eiException -> CoreCryptoException.E2eiException(this.v1)
is com.wire.crypto.uniffi.CoreCryptoException.Mls -> CoreCryptoException.Mls(this.v1.lift())
is com.wire.crypto.uniffi.CoreCryptoException.Proteus -> CoreCryptoException.Proteus(this.v1.lift())
is com.wire.crypto.uniffi.CoreCryptoException.Other -> CoreCryptoException.Other(this.v1)
}

fun com.wire.crypto.uniffi.MlsException.lift() =
Expand Down
34 changes: 21 additions & 13 deletions crypto-ffi/src/generic/context/e2ei.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{collections::HashMap, ops::DerefMut};

use crate::generic::{
context::CoreCryptoContext, Ciphersuite, ClientId, CoreCryptoResult, CrlRegistration, E2eiConversationState,
E2eiDumpedPkiEnv, E2eiEnrollment, MlsCredentialType, RotateBundle, WireIdentity,
use crate::{
generic::{
context::CoreCryptoContext, Ciphersuite, ClientId, CoreCryptoResult, CrlRegistration, E2eiConversationState,
E2eiDumpedPkiEnv, E2eiEnrollment, MlsCredentialType, RotateBundle, WireIdentity,
},
CommitBundle, CoreCryptoError,
};
use crate::CommitBundle;
use core_crypto::{prelude::VerifiableGroupInfo, CryptoError, MlsError};
use core_crypto::{prelude::VerifiableGroupInfo, RecursiveError};
use tls_codec::Deserialize;

#[uniffi::export]
Expand All @@ -20,8 +22,7 @@ impl CoreCryptoContext {
expiry_sec: u32,
ciphersuite: Ciphersuite,
) -> CoreCryptoResult<E2eiEnrollment> {
Ok(self
.context
self.context
.e2ei_new_enrollment(
client_id.into_bytes().into(),
display_name,
Expand All @@ -33,7 +34,8 @@ impl CoreCryptoContext {
.await
.map(async_lock::RwLock::new)
.map(std::sync::Arc::new)
.map(E2eiEnrollment)?)
.map(E2eiEnrollment)
.map_err(Into::into)
}

/// See [core_crypto::context::CentralContext::e2ei_new_activation_enrollment]
Expand Down Expand Up @@ -102,7 +104,7 @@ impl CoreCryptoContext {
let nb_key_package = nb_key_package
.map(usize::try_from)
.transpose()
.map_err(CryptoError::from)?;
.map_err(CoreCryptoError::generic())?;

Ok(self
.context
Expand Down Expand Up @@ -139,9 +141,13 @@ impl CoreCryptoContext {

/// See [core_crypto::context::CentralContext::e2ei_enrollment_stash]
pub async fn e2ei_enrollment_stash(&self, enrollment: std::sync::Arc<E2eiEnrollment>) -> CoreCryptoResult<Vec<u8>> {
let enrollment = std::sync::Arc::into_inner(enrollment).ok_or_else(|| CryptoError::LockPoisonError)?;
let enrollment = std::sync::Arc::into_inner(enrollment).ok_or_else(|| {
CoreCryptoError::Other("enrollment had multiple strong refs and could not be unpacked".into())
})?;
let enrollment = std::sync::Arc::into_inner(enrollment.0)
.ok_or_else(|| CryptoError::LockPoisonError)?
.ok_or_else(|| {
CoreCryptoError::Other("enrollment.0 had multiple strong refs and could not be unpacked".into())
})?
.into_inner();

Ok(self.context.e2ei_enrollment_stash(enrollment).await?)
Expand Down Expand Up @@ -221,8 +227,10 @@ impl CoreCryptoContext {
credential_type: MlsCredentialType,
) -> CoreCryptoResult<E2eiConversationState> {
let group_info = VerifiableGroupInfo::tls_deserialize(&mut group_info.as_slice())
.map_err(MlsError::from)
.map_err(CryptoError::from)?;
.map_err(core_crypto::mls::conversation::Error::tls_deserialize(
"verifiable group info",
))
.map_err(RecursiveError::mls_conversation("getting credential in use"))?;
Ok(self
.context
.get_credential_in_use(group_info, credential_type.into())
Expand Down
57 changes: 33 additions & 24 deletions crypto-ffi/src/generic/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ use super::{
MemberAddedMessages, MlsCredentialType, ProposalBundle, WelcomeBundle,
};
use async_lock::{Mutex, OnceCell};
use core_crypto::context::CentralContext;
use core_crypto::{
context::CentralContext,
prelude::{
ClientIdentifier, ConversationId, KeyPackageIn, KeyPackageRef, MlsConversationConfiguration,
VerifiableGroupInfo,
},
CryptoError, CryptoResult, MlsError,
RecursiveError,
};
use std::future::Future;
use std::{ops::Deref, sync::Arc};
use std::{future::Future, ops::Deref, sync::Arc};
use tls_codec::{Deserialize, Serialize};

pub mod e2ei;
Expand Down Expand Up @@ -191,10 +190,11 @@ impl CoreCrypto {
}
impl CoreCrypto {
/// For internal use in deprecated functions.
pub(crate) async fn deprecated_transaction<F, Fut, R>(&self, callback: F) -> CoreCryptoResult<R>
pub(crate) async fn deprecated_transaction<F, Fut, R, E>(&self, callback: F) -> CoreCryptoResult<R>
where
F: FnOnce(CentralContext) -> Fut,
Fut: Future<Output = CryptoResult<R>>,
Fut: Future<Output = Result<R, E>>,
E: Into<core_crypto::Error>,
{
let context = self.central.new_transaction().await?;
let result = callback(context.clone()).await;
Expand All @@ -205,7 +205,7 @@ impl CoreCrypto {
}
Err(err) => {
context.abort().await?;
Err(err.into())
Err(<E as Into<core_crypto::Error>>::into(err).into())
}
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ impl CoreCryptoContext {
let nb_key_package = nb_key_package
.map(usize::try_from)
.transpose()
.map_err(CryptoError::from)?;
.map_err(CoreCryptoError::generic())?;
self.context
.mls_init(
ClientIdentifier::Basic(client_id.0),
Expand Down Expand Up @@ -330,14 +330,15 @@ impl CoreCryptoContext {
let kps = self
.context
.get_or_create_client_keypackages(ciphersuite.into(), credential_type.into(), amount_requested as usize)
.await?;
.await
.map_err(RecursiveError::mls_client("getting or creating client keypackages"))?;

kps.into_iter()
.map(|kp| {
Ok(kp
.tls_serialize_detached()
.map_err(MlsError::from)
.map_err(CryptoError::from)?)
kp.tls_serialize_detached()
.map_err(core_crypto::mls::conversation::Error::tls_serialize("keypackage"))
.map_err(RecursiveError::mls_conversation("serializing keypackage"))
.map_err(Into::into)
})
.collect::<CoreCryptoResult<Vec<Vec<u8>>>>()
}
Expand All @@ -351,7 +352,8 @@ impl CoreCryptoContext {
let count = self
.context
.client_valid_key_packages_count(ciphersuite.into(), credential_type.into())
.await?;
.await
.map_err(RecursiveError::mls_client("counting client valid keypackages"))?;

Ok(count.try_into().unwrap_or(0))
}
Expand All @@ -363,7 +365,10 @@ impl CoreCryptoContext {
.map(|r| KeyPackageRef::from_slice(&r))
.collect::<Vec<_>>();

self.context.delete_keypackages(&refs[..]).await?;
self.context
.delete_keypackages(&refs[..])
.await
.map_err(RecursiveError::mls_client("deleting keypackages"))?;
Ok(())
}

Expand Down Expand Up @@ -414,7 +419,9 @@ impl CoreCryptoContext {
.into_iter()
.map(|kp| {
KeyPackageIn::tls_deserialize(&mut kp.as_slice())
.map_err(|e| CoreCryptoError::from(CryptoError::MlsError(e.into())))
.map_err(core_crypto::mls::conversation::Error::tls_deserialize("keypackage"))
.map_err(RecursiveError::mls_conversation("adding members to conversation"))
.map_err(Into::into)
})
.collect::<CoreCryptoResult<Vec<_>>>()?;

Expand Down Expand Up @@ -496,8 +503,8 @@ impl CoreCryptoContext {
keypackage: Vec<u8>,
) -> CoreCryptoResult<ProposalBundle> {
let kp = KeyPackageIn::tls_deserialize(&mut keypackage.as_slice())
.map_err(MlsError::from)
.map_err(CryptoError::from)?;
.map_err(core_crypto::mls::conversation::Error::tls_deserialize("keypackage"))
.map_err(RecursiveError::mls_conversation("creating new add proposal"))?;
self.context
.new_add_proposal(&conversation_id, kp.into())
.await?
Expand Down Expand Up @@ -529,8 +536,7 @@ impl CoreCryptoContext {
ciphersuite: Ciphersuite,
credential_type: MlsCredentialType,
) -> CoreCryptoResult<Vec<u8>> {
Ok(self
.context
self.context
.new_external_add_proposal(
conversation_id,
epoch.into(),
Expand All @@ -539,8 +545,9 @@ impl CoreCryptoContext {
)
.await?
.to_bytes()
.map_err(MlsError::from)
.map_err(CryptoError::from)?)
.map_err(core_crypto::MlsError::wrap("creating new external add proposal"))
.map_err(core_crypto::Error::Mls)
.map_err(Into::into)
}

/// See [core_crypto::context::CentralContext::join_by_external_commit]
Expand All @@ -551,8 +558,10 @@ impl CoreCryptoContext {
credential_type: MlsCredentialType,
) -> CoreCryptoResult<ConversationInitBundle> {
let group_info = VerifiableGroupInfo::tls_deserialize(&mut group_info.as_slice())
.map_err(MlsError::from)
.map_err(CryptoError::from)?;
.map_err(core_crypto::mls::conversation::Error::tls_deserialize(
"verifiable group info",
))
.map_err(RecursiveError::mls_conversation("joining by external commmit"))?;
self.context
.join_by_external_commit(group_info, custom_configuration.into(), credential_type.into())
.await?
Expand Down
Loading
Loading