From fee85ce98cae6d4f0416c4bd1c824b6c8985fad5 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 | 13 ++++++++ crates/bitwarden-wasm-internal/src/error.rs | 32 +++++++++++++++---- 7 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 crates/bitwarden-wasm-internal/src/error.js diff --git a/Cargo.lock b/Cargo.lock index 151dc1e0a..ad314fed3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -422,6 +422,7 @@ dependencies = [ "bitwarden-api-api", "bitwarden-api-identity", "bitwarden-crypto", + "bitwarden-error", "chrono", "getrandom", "hmac", @@ -703,6 +704,7 @@ version = "0.1.0" dependencies = [ "bitwarden-core", "bitwarden-crypto", + "bitwarden-error", "bitwarden-vault", "console_error_panic_hook", "console_log", diff --git a/Cargo.toml b/Cargo.toml index ce01a7b7e..ea9a82add 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 d212afb7a..d2cf7d289 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 9d9549021..d506090e4 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 33f8128e7..41639d101 100644 --- a/crates/bitwarden-wasm-internal/Cargo.toml +++ b/crates/bitwarden-wasm-internal/Cargo.toml @@ -17,6 +17,7 @@ crate-type = ["cdylib"] [dependencies] bitwarden-core = { workspace = true, features = ["wasm", "internal"] } 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 new file mode 100644 index 000000000..60adb5f63 --- /dev/null +++ b/crates/bitwarden-wasm-internal/src/error.js @@ -0,0 +1,13 @@ +/** + * Error thrown by the WASM module. + * @param {string} message - Error message. + * @extends Error + */ +class WasmError extends Error { + constructor(message, name) { + super(message); + this.name = name ?? "WasmError"; + } +} + +exports.WasmError = WasmError; diff --git a/crates/bitwarden-wasm-internal/src/error.rs b/crates/bitwarden-wasm-internal/src/error.rs index 237ee4941..3fe1df5e2 100644 --- a/crates/bitwarden-wasm-internal/src/error.rs +++ b/crates/bitwarden-wasm-internal/src/error.rs @@ -1,28 +1,48 @@ +use bitwarden_error::FlatError; use wasm_bindgen::prelude::*; // Importing an error class defined in JavaScript instead of defining it in Rust // allows us to extend the `Error` class. It also provides much better console output. -#[wasm_bindgen] +#[wasm_bindgen(module = "/src/error.js")] extern "C" { - #[wasm_bindgen(js_name = Error)] - type JsError; + type WasmError; - #[wasm_bindgen(constructor, js_class = Error)] - fn new(message: String) -> JsError; + #[wasm_bindgen(constructor)] + 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 { - JsError::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() } }