Skip to content

Conversation

dani-garcia
Copy link
Member

@dani-garcia dani-garcia commented Sep 3, 2025

🎟️ 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: Wrong Android error name when using nested external error 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:

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:

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

  • 👍 (:+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

@dani-garcia dani-garcia force-pushed the ps/use-proper-error-uniffi branch 2 times, most recently from 6303db3 to f40f671 Compare September 3, 2025 16:47

This comment was marked as resolved.

@dani-garcia dani-garcia force-pushed the ps/use-proper-error-uniffi branch from f40f671 to cdfbe13 Compare September 3, 2025 16:48
@dani-garcia dani-garcia changed the title Use proper errors in Uniffi Use actual error types in UniFFI Sep 3, 2025

This comment was marked as resolved.

Err(e) => Err(e),
}
}
) -> Result<bool, $crate::platform::repository::RepositoryError>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest UniFFI doesn't allow default trait methods, so I've removed it. We weren't using it anyway.


quote! {
#[derive(serde::Serialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is different than what WASM is doing. The WASM part is generating the code based on the wasm feature flag of the macro crate itself, while the UniFFI is always emmitting the cfg_attr code, which means it's the feature flag of the final crate that gets evaluated.

I've needed to do this because I couldn't quite get it working the other way, I think the workspace feature unification was causing issues and generating the UniFFI code all the time, even when not needed (like for the bitwarden-ipc crate).

Doing it this new way has one negative effect, which is that it's not obvious that the final crate has to have a uniffi feature, and otherwise you'll get a warning (that I've had to silence in the root cargo.toml because of wasm-only crates like bitwarden-ipc)

@dani-garcia dani-garcia changed the title Use actual error types in UniFFI [PM-23785] Use actual error types in UniFFI Sep 4, 2025
# Conflicts:
#	crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs
#	crates/bitwarden-fido/src/authenticator.rs
#	crates/bitwarden-fido/src/client_fido.rs
#	crates/bitwarden-fido/src/lib.rs
#	crates/bitwarden-fido/src/types.rs
#	crates/bitwarden-vault/src/cipher/cipher.rs
# Conflicts:
#	crates/bitwarden-uniffi/src/crypto.rs
#	crates/bitwarden-uniffi/src/platform/mod.rs
# Conflicts:
#	crates/bitwarden-uniffi/src/error.rs
#	crates/bitwarden-uniffi/src/lib.rs
dani-garcia added a commit that referenced this pull request Sep 22, 2025
## 🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-23512

## 📔 Objective

Create the bitwarden-pm crate that will sit between bitwarden-core and
bitwarden-wasm/uniffi and will encapsulate all the password manager
functionality. Note that uniffi is still mostly reimplementing the API
due to #424, so this
doesn't change that much.

I've also made the bitwarden-pm crate reexport the clients from the
feature crates, which allows us to remove them from the wasm-crate.

## ⏰ 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
@dani-garcia dani-garcia marked this pull request as ready for review September 22, 2025 14:54
@dani-garcia dani-garcia requested a review from a team as a code owner September 22, 2025 14:54
@dani-garcia dani-garcia requested a review from a team as a code owner September 22, 2025 14:54
dereknance
dereknance previously approved these changes Sep 23, 2025
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants