From e20e2c71b0abcf5d8f3b63f41bd0b4c1349bc965 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Fri, 11 Oct 2024 15:27:23 +0200 Subject: [PATCH] feat: add support for FlatError in wasm --- Cargo.lock | 2 ++ Cargo.toml | 1 - crates/bitwarden-core/Cargo.toml | 1 + .../src/client/encryption_settings.rs | 3 ++- crates/bitwarden-wasm-internal/Cargo.toml | 1 + crates/bitwarden-wasm-internal/src/error.js | 4 +-- crates/bitwarden-wasm-internal/src/error.rs | 25 +++++++++++++++++-- 7 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 441fc0ce23..28638d1ae9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,6 +429,7 @@ dependencies = [ "bitwarden-api-api", "bitwarden-api-identity", "bitwarden-crypto", + "bitwarden-error", "chrono", "getrandom", "hmac", @@ -712,6 +713,7 @@ dependencies = [ "bitwarden", "bitwarden-core", "bitwarden-crypto", + "bitwarden-error", "bitwarden-vault", "console_error_panic_hook", "console_log", diff --git a/Cargo.toml b/Cargo.toml index ce01a7b7e1..ea9a82addc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,6 @@ uniffi = "=0.28.1" uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4"] } validator = { version = "0.18.1", features = ["derive"] } wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] } -wasm-bindgen-futures = "0.4.41" [workspace.lints.clippy] unused_async = "deny" diff --git a/crates/bitwarden-core/Cargo.toml b/crates/bitwarden-core/Cargo.toml index d212afb7a7..d2cf7d289b 100644 --- a/crates/bitwarden-core/Cargo.toml +++ b/crates/bitwarden-core/Cargo.toml @@ -49,6 +49,7 @@ wasm-bindgen = { workspace = true, optional = true } zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] } zxcvbn = { version = ">=3.0.1, <4.0", optional = true } tsify-next = { workspace = true, optional = true } +bitwarden-error = { version = "1.0.0", path = "../bitwarden-error" } [target.'cfg(not(target_arch="wasm32"))'.dependencies] # By default, we use rustls as the TLS stack and rust-platform-verifier to support user-installed root certificates diff --git a/crates/bitwarden-core/src/client/encryption_settings.rs b/crates/bitwarden-core/src/client/encryption_settings.rs index 9d95490212..d506090e47 100644 --- a/crates/bitwarden-core/src/client/encryption_settings.rs +++ b/crates/bitwarden-core/src/client/encryption_settings.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use bitwarden_crypto::{AsymmetricCryptoKey, CryptoError, KeyContainer, SymmetricCryptoKey}; #[cfg(feature = "internal")] use bitwarden_crypto::{AsymmetricEncString, EncString, MasterKey}; +use bitwarden_error::prelude::*; use thiserror::Error; use uuid::Uuid; @@ -10,7 +11,7 @@ use uuid::Uuid; use crate::error::Result; use crate::VaultLocked; -#[derive(Debug, Error)] +#[derive(Debug, Error, FlatError)] pub enum EncryptionSettingsError { #[error("Cryptography error, {0}")] Crypto(#[from] bitwarden_crypto::CryptoError), diff --git a/crates/bitwarden-wasm-internal/Cargo.toml b/crates/bitwarden-wasm-internal/Cargo.toml index 522526984d..480eb54ab7 100644 --- a/crates/bitwarden-wasm-internal/Cargo.toml +++ b/crates/bitwarden-wasm-internal/Cargo.toml @@ -18,6 +18,7 @@ crate-type = ["cdylib"] bitwarden = { workspace = true, features = ["internal", "wasm"] } bitwarden-core = { workspace = true, features = ["wasm"] } bitwarden-crypto = { workspace = true, features = ["wasm"] } +bitwarden-error = { version = "1.0.0", path = "../bitwarden-error" } bitwarden-vault = { workspace = true, features = ["wasm"] } console_error_panic_hook = "0.1.7" console_log = { version = "1.0.0", features = ["color"] } diff --git a/crates/bitwarden-wasm-internal/src/error.js b/crates/bitwarden-wasm-internal/src/error.js index cd3d3a346e..60adb5f638 100644 --- a/crates/bitwarden-wasm-internal/src/error.js +++ b/crates/bitwarden-wasm-internal/src/error.js @@ -4,9 +4,9 @@ * @extends Error */ class WasmError extends Error { - constructor(message) { + constructor(message, name) { super(message); - this.name = "WasmError"; + this.name = name ?? "WasmError"; } } diff --git a/crates/bitwarden-wasm-internal/src/error.rs b/crates/bitwarden-wasm-internal/src/error.rs index 2750d26292..3fe1df5e2b 100644 --- a/crates/bitwarden-wasm-internal/src/error.rs +++ b/crates/bitwarden-wasm-internal/src/error.rs @@ -1,3 +1,4 @@ +use bitwarden_error::FlatError; use wasm_bindgen::prelude::*; // Importing an error class defined in JavaScript instead of defining it in Rust @@ -7,21 +8,41 @@ extern "C" { type WasmError; #[wasm_bindgen(constructor)] - fn new(message: String) -> WasmError; + fn new(message: String, name: Option) -> WasmError; } pub type Result = std::result::Result; pub struct GenericError(pub String); +pub struct FlattenedError { + pub variant: String, + pub message: String, +} + impl From for GenericError { fn from(error: T) -> Self { GenericError(error.to_string()) } } +impl From for FlattenedError { + fn from(error: T) -> Self { + FlattenedError { + variant: error.get_variant().to_owned(), + message: error.get_message().to_owned(), + } + } +} + impl From for JsValue { fn from(error: GenericError) -> Self { - WasmError::new(error.0).into() + WasmError::new(error.0, None).into() + } +} + +impl From for JsValue { + fn from(error: FlattenedError) -> Self { + WasmError::new(error.message, Some(error.variant)).into() } }