Skip to content

Commit

Permalink
Revert error structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Oct 11, 2024
1 parent 9dc0e90 commit f4393d6
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 22 deletions.
41 changes: 38 additions & 3 deletions crates/bitwarden-uniffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -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<bitwarden_core::Error> for BitwardenError {
fn from(e: bitwarden_core::Error) -> Self {

Check warning on line 15 in crates/bitwarden-uniffi/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/error.rs#L15

Added line #L15 was not covered by tests
Self::E(e.into())
}
}

impl From<Error> for BitwardenError {
fn from(e: Error) -> Self {

Check warning on line 21 in crates/bitwarden-uniffi/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-uniffi/src/error.rs#L21

Added line #L21 was not covered by tests
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<T, E = BitwardenError> = std::result::Result<T, E>;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Core(#[from] bitwarden_core::Error),

Expand Down Expand Up @@ -42,5 +79,3 @@ pub enum BitwardenError {
#[error(transparent)]
Fido2Client(#[from] bitwarden_fido::Fido2ClientError),
}

pub type Result<T, E = BitwardenError> = std::result::Result<T, E>;
38 changes: 30 additions & 8 deletions crates/bitwarden-uniffi/src/platform/fido2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client>);
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand Down
33 changes: 27 additions & 6 deletions crates/bitwarden-uniffi/src/tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,17 +22,33 @@ pub struct ClientGenerators(pub(crate) Arc<Client>);
impl ClientGenerators {
/// **API Draft:** Generate Password
pub fn password(&self, settings: PasswordGeneratorRequest) -> Result<String> {
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<String> {
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<String> {
Ok(self.0 .0.generator().username(settings).await?)
Ok(self
.0
.0
.generator()
.username(settings)
.await
.map_err(Error::UsernameError)?)
}
}

Expand All @@ -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
Expand All @@ -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)?)
}
}
5 changes: 3 additions & 2 deletions crates/bitwarden-uniffi/src/vault/ciphers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client>);
Expand Down Expand Up @@ -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)?)
}
}
19 changes: 16 additions & 3 deletions crates/bitwarden-uniffi/src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +51,12 @@ impl ClientVault {
/// - OTP Auth URI
/// - Steam URI
pub fn generate_totp(&self, key: String, time: Option<DateTime<Utc>>) -> Result<TotpResponse> {
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.
Expand All @@ -57,6 +65,11 @@ impl ClientVault {
view: CipherListView,
time: Option<DateTime<Utc>>,
) -> Result<TotpResponse> {
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)?)
}
}

0 comments on commit f4393d6

Please sign in to comment.