Skip to content

Commit c975847

Browse files
authored
[PM-23785] Use actual error types in UniFFI (#424)
## 🎟️ Tracking https://bitwarden.atlassian.net/browse/PM-23785 ## 📔 Objective Currently all the UniFFI errors get serialized as a string. This PR updates UniFFI to the latest main changes to add support for proper errors. This requires a couple of changes: - I've tagged all the errors with `uniffi::Error` and added support to the bitwarden-error macro for uniffi. - I've had to add some workaround code to deal with an edge case that caused some compilation issues, more info upstream: mozilla/uniffi-rs#2636 - Updated the gradle wrapper included as the old version was having some issues with my java version How this looks on the mobile apps: Android: ```kt try { client.crypto().deriveKeyConnector(request) } catch (ex: BitwardenException.DeriveKeyConnector) { when (ex.v1) { is DeriveKeyConnectorException.WrongPassword -> { println("That is the wrong password: $ex") } else -> { println("Other bitwarden error: ${ex.v1}") } } } catch (ex: BitwardenException) { println("Other bitwarden error: $ex") } ``` iOS: ```swift do { try client.crypto().deriveKeyConnector(request); } catch(BitwardenError.DeriveKeyConnector(DeriveKeyConnectorError.WrongPassword(let msg))) { print("Wrong password:", msg) } catch { print("Other bitwarden error:", error) } ``` ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent d0262d3 commit c975847

File tree

35 files changed

+269
-344
lines changed

35 files changed

+269
-344
lines changed

Cargo.lock

Lines changed: 125 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ tokio = { version = "1.36.0", features = ["macros"] }
7878
tsify = { version = ">=0.5.5, <0.6", features = [
7979
"js",
8080
], default-features = false }
81-
uniffi = "=0.29.1"
81+
uniffi = "=0.29.4"
8282
uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4", "js"] }
8383
validator = { version = ">=0.18.1, <0.21", features = ["derive"] }
8484
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
@@ -92,13 +92,23 @@ wiremock = ">=0.6.0, <0.7"
9292
[patch.crates-io]
9393
pkcs5 = { git = "https://github.com/bitwarden/rustcrypto-formats.git", rev = "2b27c63034217dd126bbf5ed874da51b84f8c705" }
9494

95+
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "6d46b3f756dde3213357c477d86771a0fc5da7b4" }
96+
uniffi_core = { git = "https://github.com/mozilla/uniffi-rs", rev = "6d46b3f756dde3213357c477d86771a0fc5da7b4" }
97+
uniffi_macros = { git = "https://github.com/mozilla/uniffi-rs", rev = "6d46b3f756dde3213357c477d86771a0fc5da7b4" }
98+
uniffi_internal_macros = { git = "https://github.com/mozilla/uniffi-rs", rev = "6d46b3f756dde3213357c477d86771a0fc5da7b4" }
99+
uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs", rev = "6d46b3f756dde3213357c477d86771a0fc5da7b4" }
100+
uniffi_build = { git = "https://github.com/mozilla/uniffi-rs", rev = "6d46b3f756dde3213357c477d86771a0fc5da7b4" }
101+
95102
[workspace.lints.clippy]
96103
unused_async = "deny"
97104
unwrap_used = "deny"
98105
string_slice = "warn"
99106

100107
[workspace.lints.rust]
101108
missing_docs = "warn"
109+
unexpected_cfgs = { level = "warn", check-cfg = [
110+
'cfg(feature, values("uniffi", "wasm"))',
111+
] }
102112

103113
[workspace.metadata.dylint]
104114
libraries = [{ path = "support/lints" }]

crates/bitwarden-auth/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ license-file.workspace = true
1515
keywords.workspace = true
1616

1717
[features]
18-
uniffi = ["bitwarden-core/uniffi", "dep:uniffi"] # Uniffi bindings
1918
wasm = [
2019
"bitwarden-core/wasm",
2120
"dep:tsify",
@@ -32,7 +31,6 @@ reqwest = { workspace = true }
3231
serde = { workspace = true }
3332
thiserror = { workspace = true }
3433
tsify = { workspace = true, optional = true }
35-
uniffi = { workspace = true, optional = true }
3634
wasm-bindgen = { workspace = true, optional = true }
3735
wasm-bindgen-futures = { workspace = true, optional = true }
3836

crates/bitwarden-core/src/auth/auth_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl AuthClient {
206206
#[allow(missing_docs)]
207207
#[cfg(feature = "internal")]
208208
#[derive(Debug, thiserror::Error)]
209+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
209210
pub enum TrustDeviceError {
210211
#[error(transparent)]
211212
Crypto(#[from] bitwarden_crypto::CryptoError),

crates/bitwarden-core/src/auth/auth_request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub(crate) fn auth_request_decrypt_master_key(
7575

7676
#[allow(missing_docs)]
7777
#[derive(Debug, Error)]
78+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
7879
pub enum ApproveAuthRequestError {
7980
#[error(transparent)]
8081
Crypto(#[from] CryptoError),

crates/bitwarden-core/src/auth/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub use key_connector::KeyConnectorResponse;
5151
/// Error for authentication related operations
5252
#[allow(missing_docs)]
5353
#[derive(Debug, Error)]
54+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
5455
pub enum AuthValidateError {
5556
#[error(transparent)]
5657
NotAuthenticated(#[from] NotAuthenticatedError),

crates/bitwarden-core/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ macro_rules! impl_bitwarden_error {
3030
/// Errors from performing network requests.
3131
#[allow(missing_docs)]
3232
#[derive(Debug, Error)]
33+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
3334
pub enum ApiError {
3435
#[error(transparent)]
3536
Reqwest(#[from] reqwest::Error),

crates/bitwarden-core/src/platform/generate_fingerprint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct FingerprintResponse {
3333
/// Errors that can occur when computing a fingerprint.
3434
#[allow(missing_docs)]
3535
#[derive(Debug, Error)]
36+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
3637
pub enum FingerprintError {
3738
#[error(transparent)]
3839
Crypto(#[from] bitwarden_crypto::CryptoError),
@@ -48,6 +49,7 @@ pub(crate) fn generate_fingerprint(input: &FingerprintRequest) -> Result<String,
4849
/// Errors that can occur when computing a fingerprint.
4950
#[allow(missing_docs)]
5051
#[derive(Debug, Error)]
52+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
5153
pub enum UserFingerprintError {
5254
#[error(transparent)]
5355
Crypto(#[from] bitwarden_crypto::CryptoError),

crates/bitwarden-error-macro/src/flat/attribute.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub(crate) fn bitwarden_error_flat(
4141
});
4242

4343
quote! {
44+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
4445
#input
4546
#wasm
4647

crates/bitwarden-error-macro/src/full/attribute.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(crate) fn bitwarden_error_full(
2121

2222
quote! {
2323
#[derive(serde::Serialize)]
24+
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
2425
#wasm_attributes
2526
#input
2627
}

0 commit comments

Comments
 (0)