From f4393d6ab6b88a5f69546345fd5edc3f55f33d51 Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 11 Oct 2024 10:31:20 +0200 Subject: [PATCH] Revert error structure --- crates/bitwarden-uniffi/src/error.rs | 41 +++++++++++++++++-- crates/bitwarden-uniffi/src/platform/fido2.rs | 38 +++++++++++++---- crates/bitwarden-uniffi/src/tool/mod.rs | 33 ++++++++++++--- crates/bitwarden-uniffi/src/vault/ciphers.rs | 5 ++- crates/bitwarden-uniffi/src/vault/mod.rs | 19 +++++++-- 5 files changed, 114 insertions(+), 22 deletions(-) diff --git a/crates/bitwarden-uniffi/src/error.rs b/crates/bitwarden-uniffi/src/error.rs index abcd4a4f5..889e3c7fb 100644 --- a/crates/bitwarden-uniffi/src/error.rs +++ b/crates/bitwarden-uniffi/src/error.rs @@ -1,11 +1,48 @@ +use std::fmt::{Display, Formatter}; + use bitwarden_exporters::ExportError; use bitwarden_generators::{PassphraseError, PasswordError, UsernameError}; // Name is converted from *Error to *Exception, so we can't just name the enum Error because // Exception already exists -#[derive(uniffi::Error, thiserror::Error, Debug)] +#[derive(uniffi::Error, Debug)] #[uniffi(flat_error)] pub enum BitwardenError { + E(Error), +} + +impl From for BitwardenError { + fn from(e: bitwarden_core::Error) -> Self { + Self::E(e.into()) + } +} + +impl From for BitwardenError { + fn from(e: Error) -> Self { + Self::E(e) + } +} + +impl Display for BitwardenError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::E(e) => Display::fmt(e, f), + } + } +} + +impl std::error::Error for BitwardenError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + BitwardenError::E(e) => Some(e), + } + } +} + +pub type Result = std::result::Result; + +#[derive(thiserror::Error, Debug)] +pub enum Error { #[error(transparent)] Core(#[from] bitwarden_core::Error), @@ -42,5 +79,3 @@ pub enum BitwardenError { #[error(transparent)] Fido2Client(#[from] bitwarden_fido::Fido2ClientError), } - -pub type Result = std::result::Result; diff --git a/crates/bitwarden-uniffi/src/platform/fido2.rs b/crates/bitwarden-uniffi/src/platform/fido2.rs index e484f5f2e..99c998648 100644 --- a/crates/bitwarden-uniffi/src/platform/fido2.rs +++ b/crates/bitwarden-uniffi/src/platform/fido2.rs @@ -9,7 +9,10 @@ use bitwarden_fido::{ }; use bitwarden_vault::{Cipher, CipherView, Fido2CredentialNewView}; -use crate::{error::Result, Client}; +use crate::{ + error::{Error, Result}, + Client, +}; #[derive(uniffi::Object)] pub struct ClientFido2(pub(crate) Arc); @@ -48,7 +51,8 @@ impl ClientFido2 { .0 .0 .fido2() - .decrypt_fido2_autofill_credentials(cipher_view)?; + .decrypt_fido2_autofill_credentials(cipher_view) + .map_err(Error::DecryptFido2AutofillCredentialsError)?; Ok(result) } @@ -72,7 +76,10 @@ impl ClientFido2Authenticator { let cs = UniffiTraitBridge(self.2.as_ref()); let mut auth = fido2.create_authenticator(&ui, &cs); - let result = auth.make_credential(request).await?; + let result = auth + .make_credential(request) + .await + .map_err(Error::MakeCredential)?; Ok(result) } @@ -82,7 +89,10 @@ impl ClientFido2Authenticator { let cs = UniffiTraitBridge(self.2.as_ref()); let mut auth = fido2.create_authenticator(&ui, &cs); - let result = auth.get_assertion(request).await?; + let result = auth + .get_assertion(request) + .await + .map_err(Error::GetAssertion)?; Ok(result) } @@ -96,7 +106,10 @@ impl ClientFido2Authenticator { let cs = UniffiTraitBridge(self.2.as_ref()); let mut auth = fido2.create_authenticator(&ui, &cs); - let result = auth.silently_discover_credentials(rp_id).await?; + let result = auth + .silently_discover_credentials(rp_id) + .await + .map_err(Error::SilentlyDiscoverCredentials)?; Ok(result) } @@ -106,7 +119,10 @@ impl ClientFido2Authenticator { let cs = UniffiTraitBridge(self.2.as_ref()); let mut auth = fido2.create_authenticator(&ui, &cs); - let result = auth.credentials_for_autofill().await?; + let result = auth + .credentials_for_autofill() + .await + .map_err(Error::CredentialsForAutofillError)?; Ok(result) } } @@ -127,7 +143,10 @@ impl ClientFido2Client { let cs = UniffiTraitBridge(self.0 .2.as_ref()); let mut client = fido2.create_client(&ui, &cs); - let result = client.register(origin, request, client_data).await?; + let result = client + .register(origin, request, client_data) + .await + .map_err(Error::Fido2Client)?; Ok(result) } @@ -142,7 +161,10 @@ impl ClientFido2Client { let cs = UniffiTraitBridge(self.0 .2.as_ref()); let mut client = fido2.create_client(&ui, &cs); - let result = client.authenticate(origin, request, client_data).await?; + let result = client + .authenticate(origin, request, client_data) + .await + .map_err(Error::Fido2Client)?; Ok(result) } } diff --git a/crates/bitwarden-uniffi/src/tool/mod.rs b/crates/bitwarden-uniffi/src/tool/mod.rs index 4b96c37d1..498884218 100644 --- a/crates/bitwarden-uniffi/src/tool/mod.rs +++ b/crates/bitwarden-uniffi/src/tool/mod.rs @@ -7,7 +7,10 @@ use bitwarden_generators::{ }; use bitwarden_vault::{Cipher, Collection, Folder}; -use crate::{error::Result, Client}; +use crate::{ + error::{Error, Result}, + Client, +}; mod sends; pub use sends::ClientSends; @@ -19,17 +22,33 @@ pub struct ClientGenerators(pub(crate) Arc); impl ClientGenerators { /// **API Draft:** Generate Password pub fn password(&self, settings: PasswordGeneratorRequest) -> Result { - Ok(self.0 .0.generator().password(settings)?) + Ok(self + .0 + .0 + .generator() + .password(settings) + .map_err(Error::PasswordError)?) } /// **API Draft:** Generate Passphrase pub fn passphrase(&self, settings: PassphraseGeneratorRequest) -> Result { - Ok(self.0 .0.generator().passphrase(settings)?) + Ok(self + .0 + .0 + .generator() + .passphrase(settings) + .map_err(Error::PassphraseError)?) } /// **API Draft:** Generate Username pub async fn username(&self, settings: UsernameGeneratorRequest) -> Result { - Ok(self.0 .0.generator().username(settings).await?) + Ok(self + .0 + .0 + .generator() + .username(settings) + .await + .map_err(Error::UsernameError)?) } } @@ -49,7 +68,8 @@ impl ClientExporters { .0 .0 .exporters() - .export_vault(folders, ciphers, format)?) + .export_vault(folders, ciphers, format) + .map_err(Error::ExportError)?) } /// **API Draft:** Export organization vault @@ -63,6 +83,7 @@ impl ClientExporters { .0 .0 .exporters() - .export_organization_vault(collections, ciphers, format)?) + .export_organization_vault(collections, ciphers, format) + .map_err(Error::ExportError)?) } } diff --git a/crates/bitwarden-uniffi/src/vault/ciphers.rs b/crates/bitwarden-uniffi/src/vault/ciphers.rs index fa108af11..64751b37c 100644 --- a/crates/bitwarden-uniffi/src/vault/ciphers.rs +++ b/crates/bitwarden-uniffi/src/vault/ciphers.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use bitwarden_vault::{Cipher, CipherListView, CipherView, ClientVaultExt, Fido2CredentialView}; use uuid::Uuid; -use crate::{Client, Result}; +use crate::{error::Error, Client, Result}; #[derive(uniffi::Object)] pub struct ClientCiphers(pub Arc); @@ -48,6 +48,7 @@ impl ClientCiphers { .0 .vault() .ciphers() - .move_to_organization(cipher, organization_id)?) + .move_to_organization(cipher, organization_id) + .map_err(Error::Cipher)?) } } diff --git a/crates/bitwarden-uniffi/src/vault/mod.rs b/crates/bitwarden-uniffi/src/vault/mod.rs index c65177ac3..cec625e33 100644 --- a/crates/bitwarden-uniffi/src/vault/mod.rs +++ b/crates/bitwarden-uniffi/src/vault/mod.rs @@ -3,7 +3,10 @@ use std::sync::Arc; use bitwarden_vault::{CipherListView, ClientVaultExt, TotpResponse}; use chrono::{DateTime, Utc}; -use crate::{error::Result, Client}; +use crate::{ + error::{Error, Result}, + Client, +}; pub mod attachments; pub mod ciphers; @@ -48,7 +51,12 @@ impl ClientVault { /// - OTP Auth URI /// - Steam URI pub fn generate_totp(&self, key: String, time: Option>) -> Result { - Ok(self.0 .0.vault().generate_totp(key, time)?) + Ok(self + .0 + .0 + .vault() + .generate_totp(key, time) + .map_err(Error::Totp)?) } /// Generate a TOTP code from a provided cipher list view. @@ -57,6 +65,11 @@ impl ClientVault { view: CipherListView, time: Option>, ) -> Result { - Ok(self.0 .0.vault().generate_totp_cipher_view(view, time)?) + Ok(self + .0 + .0 + .vault() + .generate_totp_cipher_view(view, time) + .map_err(Error::Totp)?) } }