π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Own the contacts error vocabulary in the crate
Manav Rathi54425169eaf1
diff --git a/rust/bindings/frb/ente-rust/Cargo.toml b/rust/bindings/frb/ente-rust/Cargo.tomlindex c6d62f9908..f7026a08dd 100644--- a/rust/bindings/frb/ente-rust/Cargo.toml+++ b/rust/bindings/frb/ente-rust/Cargo.toml@@ -16,4 +16,3 @@ flutter = [][lints.rust]unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }-missing_docs = "warn"diff --git a/rust/bindings/frb/ente-rust/src/api/contacts.rs b/rust/bindings/frb/ente-rust/src/api/contacts.rsindex c72c6f82e0..1adfb8a03e 100644--- a/rust/bindings/frb/ente-rust/src/api/contacts.rs+++ b/rust/bindings/frb/ente-rust/src/api/contacts.rs@@ -17,80 +17,37 @@ use ente_core::auth::KeyAttributes as CoreKeyAttributes;use flutter_rust_bridge::frb;#[frb]-/// Contact API errors exposed over Flutter Rust Bridge.pub enum ContactsError {- /// API HTTP error with status and message.- Http {- /// Error response body or summary message.- message: String,- /// HTTP status code returned by the API.- status: u16,- },- /// Network transport error.- Network {- /// Underlying network error description.- message: String,- },- /// Response or payload parse error.- Parse {- /// Parse failure description.- message: String,- },- /// Invalid request or base URL error.- InvalidUrl {- /// Invalid URL description.- message: String,- },- /// Cryptographic operation failed.- Crypto {- /// Cryptographic error description.- message: String,- },- /// Authentication or recovery crypto operation failed.- Auth {- /// Authentication error description.- message: String,- },- /// Input validation failed.- InvalidInput {- /// Validation failure description.- message: String,- },- /// Missing encrypted contact payload on a live contact response.- MissingEncryptedData,- /// Missing encrypted contact key on a live contact response.- MissingEncryptedKey,- /// Contact profile picture does not exist.- ProfilePictureNotFound,- /// A recovery is already in progress for this kit.- ActiveRecoverySession,+ Network { message: String },+ Http { status: u16, message: String },+ Parse { message: String },+ Crypto { message: String },+ Auth { message: String },+ InvalidInput { message: String },+ MissingEncryptedData { message: String },+ MissingEncryptedKey { message: String },+ ProfilePictureNotFound { message: String },+ ActiveRecoverySession { message: String },}impl From<CoreContactsError> for ContactsError {- fn from(value: CoreContactsError) -> Self {- match value {- CoreContactsError::Http(error) => {- let message = ente_core::error::chain(&error);- match error {- ente_core::http::Error::Http { status, .. }- | ente_core::http::Error::Api { status, .. } => {- ContactsError::Http { message, status }- }- ente_core::http::Error::Network(_) => ContactsError::Network { message },- ente_core::http::Error::Parse(_) => ContactsError::Parse { message },- }- }- CoreContactsError::Crypto(message) => ContactsError::Crypto {- message: message.to_string(),- },- CoreContactsError::Auth(message) => ContactsError::Auth {- message: message.to_string(),+ fn from(e: CoreContactsError) -> Self {+ use ente_contacts::ErrorKind as K;+ let message = ente_core::error::chain(&e);+ match e.kind() {+ K::Network => Self::Network { message },+ K::Http => Self::Http {+ status: e.status().unwrap_or_default(),+ message,},- CoreContactsError::InvalidInput(message) => ContactsError::InvalidInput { message },- CoreContactsError::MissingEncryptedData => ContactsError::MissingEncryptedData,- CoreContactsError::MissingEncryptedKey => ContactsError::MissingEncryptedKey,- CoreContactsError::ProfilePictureNotFound => ContactsError::ProfilePictureNotFound,- CoreContactsError::ActiveRecoverySession => ContactsError::ActiveRecoverySession,+ K::Parse => Self::Parse { message },+ K::Crypto => Self::Crypto { message },+ K::Auth => Self::Auth { message },+ K::InvalidInput => Self::InvalidInput { message },+ K::MissingEncryptedData => Self::MissingEncryptedData { message },+ K::MissingEncryptedKey => Self::MissingEncryptedKey { message },+ K::ProfilePictureNotFound => Self::ProfilePictureNotFound { message },+ K::ActiveRecoverySession => Self::ActiveRecoverySession { message },}}}diff --git a/rust/bindings/wasm/ente-wasm/Cargo.toml b/rust/bindings/wasm/ente-wasm/Cargo.tomlindex 3a03eaba7c..f578b94376 100644--- a/rust/bindings/wasm/ente-wasm/Cargo.toml+++ b/rust/bindings/wasm/ente-wasm/Cargo.toml@@ -14,6 +14,3 @@ serde.workspace = trueserde-wasm-bindgen = "0.6"wasm-bindgen.workspace = truewasm-bindgen-futures = "0.4"--[lints.rust]-missing_docs = "warn"diff --git a/rust/bindings/wasm/ente-wasm/src/contacts.rs b/rust/bindings/wasm/ente-wasm/src/contacts.rsindex 41c1aaf03e..ae83ae10eb 100644--- a/rust/bindings/wasm/ente-wasm/src/contacts.rs+++ b/rust/bindings/wasm/ente-wasm/src/contacts.rs@@ -10,53 +10,54 @@ use serde::{Deserialize, Serialize};use serde_wasm_bindgen as swb;use wasm_bindgen::prelude::*;-/// Contacts error.-#[wasm_bindgen]-pub struct ContactsError {- code: String,- message: String,+#[derive(Serialize)]+#[serde(tag = "kind", rename_all = "snake_case")]+pub enum ContactsError {+ Network { message: String },+ Http { status: u16, message: String },+ Parse { message: String },+ Crypto { message: String },+ Auth { message: String },+ InvalidInput { message: String },+ MissingEncryptedData { message: String },+ MissingEncryptedKey { message: String },+ ProfilePictureNotFound { message: String },+ ActiveRecoverySession { message: String },+ Serde { message: String },+ Decode { message: String },}-#[wasm_bindgen]-impl ContactsError {- /// Machine-readable error code.- #[wasm_bindgen(getter)]- pub fn code(&self) -> String {- self.code.clone()- }-- /// Human-readable error message.- #[wasm_bindgen(getter)]- pub fn message(&self) -> String {- self.message.clone()+impl From<ContactsError> for JsValue {+ fn from(e: ContactsError) -> Self {+ swb::to_value(&e).unwrap_or_else(|err| JsValue::from_str(&err.to_string()))}}impl From<CoreContactsError> for ContactsError {fn from(e: CoreContactsError) -> Self {- let code = match &e {- CoreContactsError::Http(_) => "http",- CoreContactsError::Crypto(_) => "crypto",- CoreContactsError::Auth(_) => "auth",- CoreContactsError::InvalidInput(_) => "invalid_input",- CoreContactsError::MissingEncryptedData => "missing_encrypted_data",- CoreContactsError::MissingEncryptedKey => "missing_encrypted_key",- CoreContactsError::ProfilePictureNotFound => "profile_picture_not_found",- CoreContactsError::ActiveRecoverySession => "active_recovery_session",- }- .to_string();-- Self {- code,- message: ente_core::error::chain(&e),+ use ente_contacts::ErrorKind as K;+ let message = ente_core::error::chain(&e);+ match e.kind() {+ K::Network => Self::Network { message },+ K::Http => Self::Http {+ status: e.status().unwrap_or_default(),+ message,+ },+ K::Parse => Self::Parse { message },+ K::Crypto => Self::Crypto { message },+ K::Auth => Self::Auth { message },+ K::InvalidInput => Self::InvalidInput { message },+ K::MissingEncryptedData => Self::MissingEncryptedData { message },+ K::MissingEncryptedKey => Self::MissingEncryptedKey { message },+ K::ProfilePictureNotFound => Self::ProfilePictureNotFound { message },+ K::ActiveRecoverySession => Self::ActiveRecoverySession { message },}}}impl From<swb::Error> for ContactsError {fn from(e: swb::Error) -> Self {- Self {- code: "serde".to_string(),+ Self::Serde {message: e.to_string(),}}@@ -117,10 +118,10 @@ impl From<ente_contacts::ContactRecord> for ContactRecordJs {#[wasm_bindgen]pub async fn contacts_open_ctx(input: JsValue) -> Result<JsValue, ContactsError> {let input: OpenContactsCtxJsInput = swb::from_value(input)?;- let master_key = crypto::decode_b64(&input.master_key_b64).map_err(|e| ContactsError {- code: "decode".to_string(),- message: e.to_string(),- })?;+ let master_key =+ crypto::decode_b64(&input.master_key_b64).map_err(|e| ContactsError::Decode {+ message: e.to_string(),+ })?;let result = ContactsCtx::open(OpenContactsCtxInput {base_url: input.base_url,diff --git a/rust/crates/accounts/Cargo.toml b/rust/crates/accounts/Cargo.tomlindex ac682c5907..1d3d490029 100644--- a/rust/crates/accounts/Cargo.toml+++ b/rust/crates/accounts/Cargo.toml@@ -17,6 +17,3 @@ mockito = "1.6"sha2 = "0.11"srp = { version = "0.7.0-rc.3", default-features = false }tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }--[lints.rust]-missing_docs = "warn"diff --git a/rust/crates/contacts/src/error.rs b/rust/crates/contacts/src/error.rsindex d7c9f9676e..f77180f73e 100644--- a/rust/crates/contacts/src/error.rs+++ b/rust/crates/contacts/src/error.rs@@ -28,4 +28,43 @@ pub enum ContactsError {ActiveRecoverySession,}+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum ErrorKind {+ Network,+ Http,+ Parse,+ Crypto,+ Auth,+ InvalidInput,+ MissingEncryptedData,+ MissingEncryptedKey,+ ProfilePictureNotFound,+ ActiveRecoverySession,+}++impl ContactsError {+ pub fn kind(&self) -> ErrorKind {+ match self {+ ContactsError::Http(http::Error::Network(_)) => ErrorKind::Network,+ ContactsError::Http(http::Error::Parse(_)) => ErrorKind::Parse,+ ContactsError::Http(_) => ErrorKind::Http,+ ContactsError::Crypto(_) => ErrorKind::Crypto,+ ContactsError::Auth(_) => ErrorKind::Auth,+ ContactsError::InvalidInput(_) => ErrorKind::InvalidInput,+ ContactsError::MissingEncryptedData => ErrorKind::MissingEncryptedData,+ ContactsError::MissingEncryptedKey => ErrorKind::MissingEncryptedKey,+ ContactsError::ProfilePictureNotFound => ErrorKind::ProfilePictureNotFound,+ ContactsError::ActiveRecoverySession => ErrorKind::ActiveRecoverySession,+ }+ }++ /// The HTTP status, when the server answered with one.+ pub fn status(&self) -> Option<u16> {+ match self {+ ContactsError::Http(error) => error.status_code(),+ _ => None,+ }+ }+}+pub type Result<T> = std::result::Result<T, ContactsError>;diff --git a/rust/crates/contacts/src/lib.rs b/rust/crates/contacts/src/lib.rsindex c2234df671..e83783b5dd 100644--- a/rust/crates/contacts/src/lib.rs+++ b/rust/crates/contacts/src/lib.rs@@ -10,7 +10,7 @@ pub mod models;pub mod transport;pub use client::{ContactsCtx, OpenContactsCtxInput, OpenContactsCtxResult, RootKeySource};-pub use error::{ContactsError, Result};+pub use error::{ContactsError, ErrorKind, Result};pub use legacy_kit::{LegacyKitRecoveryClient, LegacyKitRecoveryHandle};pub use legacy_kit_models::{LEGACY_KIT_PAYLOAD_VERSION, LegacyKit, LegacyKitCreateResult, LegacyKitMetadata,