Skip to content

Commit cc34372

Browse files
committed
Merge branch 'main' into km/pm-12400/private-key-regen
2 parents 3ac3cf6 + 130ac6f commit cc34372

File tree

32 files changed

+988
-79
lines changed

32 files changed

+988
-79
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ bitwarden-generators = { path = "crates/bitwarden-generators", version = "=1.0.0
2929
bitwarden-send = { path = "crates/bitwarden-send", version = "=1.0.0" }
3030
bitwarden-sm = { path = "bitwarden_license/bitwarden-sm", version = "=1.0.0" }
3131
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=1.0.0" }
32+
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
33+
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
3234

3335
# External crates that are expected to maintain a consistent version across all crates
3436
chrono = { version = ">=0.4.26, <0.5", features = [
@@ -56,6 +58,7 @@ uniffi = "=0.28.1"
5658
uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4"] }
5759
validator = { version = "0.18.1", features = ["derive"] }
5860
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
61+
js-sys = { version = ">=0.3.72, <0.4" }
5962
wasm-bindgen-futures = "0.4.41"
6063

6164
[workspace.lints.clippy]

crates/bitwarden-core/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ no-memory-hardening = [
2020
] # Disable memory hardening features
2121
uniffi = ["bitwarden-crypto/uniffi", "dep:uniffi"] # Uniffi bindings
2222
secrets = [] # Secrets manager API
23-
wasm = ["dep:wasm-bindgen", "dep:tsify-next"] # WASM support
23+
wasm = [
24+
"bitwarden-error/wasm",
25+
"dep:wasm-bindgen",
26+
"dep:tsify-next",
27+
] # WASM support
2428

2529
[dependencies]
2630
base64 = ">=0.22.1, <0.23"
@@ -49,6 +53,7 @@ wasm-bindgen = { workspace = true, optional = true }
4953
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }
5054
zxcvbn = { version = ">=3.0.1, <4.0", optional = true }
5155
tsify-next = { workspace = true, optional = true }
56+
bitwarden-error = { workspace = true }
5257

5358
[target.'cfg(not(target_arch="wasm32"))'.dependencies]
5459
# By default, we use rustls as the TLS stack and rust-platform-verifier to support user-installed root certificates

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ use std::collections::HashMap;
33
use bitwarden_crypto::{AsymmetricCryptoKey, CryptoError, KeyContainer, SymmetricCryptoKey};
44
#[cfg(feature = "internal")]
55
use bitwarden_crypto::{AsymmetricEncString, EncString, MasterKey};
6+
use bitwarden_error::prelude::*;
67
use thiserror::Error;
78
use uuid::Uuid;
89

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

14+
#[bitwarden_error(flat)]
1315
#[derive(Debug, Error)]
1416
pub enum EncryptionSettingsError {
1517
#[error("Cryptography error, {0}")]

crates/bitwarden-core/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{borrow::Cow, fmt::Debug};
44

55
use bitwarden_api_api::apis::Error as ApiError;
66
use bitwarden_api_identity::apis::Error as IdentityError;
7+
use bitwarden_error::prelude::*;
78
use log::debug;
89
use reqwest::StatusCode;
910
use thiserror::Error;
@@ -12,6 +13,7 @@ use validator::ValidationErrors;
1213
#[cfg(feature = "internal")]
1314
use crate::client::encryption_settings::EncryptionSettingsError;
1415

16+
#[bitwarden_error(flat, export_as = "CoreError")]
1517
#[derive(Debug, Error)]
1618
pub enum Error {
1719
#[error(transparent)]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "bitwarden-error-macro"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
license-file.workspace = true
10+
keywords.workspace = true
11+
12+
[features]
13+
wasm = ["bitwarden-error/wasm"]
14+
15+
[dependencies]
16+
darling = "0.20.10"
17+
proc-macro2 = "1.0.89"
18+
quote = "1.0.37"
19+
syn = "2.0.87"
20+
21+
[lints]
22+
workspace = true
23+
24+
[lib]
25+
proc-macro = true
26+
27+
[dev-dependencies]
28+
bitwarden-error.workspace = true
29+
serde.workspace = true
30+
thiserror.workspace = true
31+
tsify-next.workspace = true
32+
js-sys.workspace = true
33+
wasm-bindgen.workspace = true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use darling::FromMeta;
2+
3+
#[derive(FromMeta)]
4+
pub(crate) struct BitwardenErrorArgs {
5+
#[darling(flatten)]
6+
pub error_type: BitwardenErrorType,
7+
8+
#[darling(default)]
9+
pub export_as: Option<String>,
10+
}
11+
12+
#[derive(FromMeta)]
13+
#[darling(rename_all = "snake_case")]
14+
pub(crate) enum BitwardenErrorType {
15+
/// The error is going to be converted into a string using the `ToString` trait
16+
Basic,
17+
18+
/// The error is going to be converted into a flat error using the `FlatError` trait
19+
Flat,
20+
21+
/// The entire error stack is going to be made available using `serde`
22+
Full,
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use darling::{ast::NestedMeta, FromMeta};
2+
use quote::format_ident;
3+
4+
use crate::args::{BitwardenErrorArgs, BitwardenErrorType};
5+
6+
pub(crate) fn bitwarden_error(
7+
args: proc_macro::TokenStream,
8+
item: proc_macro::TokenStream,
9+
) -> proc_macro::TokenStream {
10+
let attr_args = match NestedMeta::parse_meta_list(args.into()) {
11+
Ok(v) => v,
12+
Err(e) => {
13+
return proc_macro::TokenStream::from(darling::Error::from(e).write_errors());
14+
}
15+
};
16+
17+
let args = match BitwardenErrorArgs::from_list(&attr_args) {
18+
Ok(params) => params,
19+
Err(error) => {
20+
return proc_macro::TokenStream::from(error.write_errors());
21+
}
22+
};
23+
24+
let input = syn::parse_macro_input!(item as syn::DeriveInput);
25+
let type_identifier = &input.ident;
26+
let export_as_identifier = &args
27+
.export_as
28+
.as_ref()
29+
.map(|export_as| format_ident!("{}", export_as))
30+
.unwrap_or(input.ident.clone());
31+
32+
match args.error_type {
33+
BitwardenErrorType::Basic => crate::basic::attribute::bitwarden_error_basic(
34+
&input,
35+
type_identifier,
36+
export_as_identifier,
37+
),
38+
BitwardenErrorType::Flat => crate::flat::attribute::bitwarden_error_flat(
39+
&input,
40+
type_identifier,
41+
export_as_identifier,
42+
),
43+
BitwardenErrorType::Full => crate::full::attribute::bitwarden_error_full(
44+
&input,
45+
type_identifier,
46+
export_as_identifier,
47+
),
48+
}
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use quote::quote;
2+
3+
pub(crate) fn bitwarden_error_basic(
4+
input: &syn::DeriveInput,
5+
type_identifier: &proc_macro2::Ident,
6+
export_as_identifier: &proc_macro2::Ident,
7+
) -> proc_macro::TokenStream {
8+
let wasm =
9+
cfg!(feature = "wasm").then(|| basic_error_wasm(type_identifier, export_as_identifier));
10+
quote! {
11+
#input
12+
13+
#wasm
14+
}
15+
.into()
16+
}
17+
18+
fn basic_error_wasm(
19+
type_identifier: &proc_macro2::Ident,
20+
export_as_identifier: &proc_macro2::Ident,
21+
) -> proc_macro2::TokenStream {
22+
let export_as_identifier_str = export_as_identifier.to_string();
23+
let is_error_function_name = format!("is{}", export_as_identifier);
24+
let ts_code_str = format!(
25+
r##"r#"
26+
export interface {export_as_identifier} extends Error {{
27+
name: "{export_as_identifier}";
28+
}};
29+
30+
export function {is_error_function_name}(error: any): error is {export_as_identifier};
31+
"#"##
32+
);
33+
let ts_code: proc_macro2::TokenStream = ts_code_str
34+
.parse()
35+
.expect("Could not generate TypeScript code");
36+
37+
quote! {
38+
const _: () = {
39+
use bitwarden_error::wasm_bindgen::prelude::*;
40+
41+
#[wasm_bindgen(typescript_custom_section)]
42+
const TS_APPEND_CONTENT: &'static str = #ts_code;
43+
44+
#[wasm_bindgen(js_name = #is_error_function_name, skip_typescript)]
45+
pub fn is_error(error: &JsValue) -> bool {
46+
let name_js_value = bitwarden_error::js_sys::Reflect::get(&error, &JsValue::from_str("name")).unwrap_or(JsValue::NULL);
47+
let name = name_js_value.as_string().unwrap_or_default();
48+
name == #export_as_identifier_str
49+
}
50+
51+
#[automatically_derived]
52+
impl From<#type_identifier> for JsValue {
53+
fn from(error: #type_identifier) -> Self {
54+
let js_error = SdkJsError::new(error.to_string());
55+
js_error.set_name(#export_as_identifier_str.to_owned());
56+
js_error.into()
57+
}
58+
}
59+
};
60+
}
61+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod attribute;

0 commit comments

Comments
 (0)