Skip to content

Commit

Permalink
feat: add support for FlatError in wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
coroiu committed Oct 15, 2024
1 parent 457e9b6 commit fee85ce
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion crates/bitwarden-core/src/client/encryption_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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;

#[cfg(feature = "internal")]
use crate::error::Result;
use crate::VaultLocked;

#[derive(Debug, Error)]
#[derive(Debug, Error, FlatError)]

Check warning on line 14 in crates/bitwarden-core/src/client/encryption_settings.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/client/encryption_settings.rs#L14

Added line #L14 was not covered by tests
pub enum EncryptionSettingsError {
#[error("Cryptography error, {0}")]
Crypto(#[from] bitwarden_crypto::CryptoError),
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-wasm-internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
13 changes: 13 additions & 0 deletions crates/bitwarden-wasm-internal/src/error.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 26 additions & 6 deletions crates/bitwarden-wasm-internal/src/error.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> WasmError;
}

pub type Result<T, E = GenericError> = std::result::Result<T, E>;

pub struct GenericError(pub String);

pub struct FlattenedError {
pub variant: String,
pub message: String,
}

impl<T: ToString> From<T> for GenericError {
fn from(error: T) -> Self {
GenericError(error.to_string())
}
}

impl<T: FlatError> From<T> for FlattenedError {
fn from(error: T) -> Self {
FlattenedError {
variant: error.get_variant().to_owned(),
message: error.get_message().to_owned(),
}
}

Check warning on line 35 in crates/bitwarden-wasm-internal/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/error.rs#L30-L35

Added lines #L30 - L35 were not covered by tests
}

impl From<GenericError> for JsValue {
fn from(error: GenericError) -> Self {
JsError::new(error.0).into()
WasmError::new(error.0, None).into()
}

Check warning on line 41 in crates/bitwarden-wasm-internal/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/error.rs#L40-L41

Added lines #L40 - L41 were not covered by tests
}

impl From<FlattenedError> for JsValue {
fn from(error: FlattenedError) -> Self {
WasmError::new(error.message, Some(error.variant)).into()

Check warning on line 46 in crates/bitwarden-wasm-internal/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm-internal/src/error.rs#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}

0 comments on commit fee85ce

Please sign in to comment.