Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Chain-render errors at the binding boundaries

Manav Rathi9ef577f98bf8
diff --git a/rust/bindings/frb/ente-rust/src/api/contacts.rs b/rust/bindings/frb/ente-rust/src/api/contacts.rs
index bdaa9c7234..c72c6f82e0 100644
--- a/rust/bindings/frb/ente-rust/src/api/contacts.rs
+++ b/rust/bindings/frb/ente-rust/src/api/contacts.rs
@@ -1,6 +1,5 @@
//! FRB bindings for contacts APIs.
-use std::fmt::Write as _;
use std::sync::Arc;
use ente_contacts::{
@@ -71,7 +70,7 @@ impl From<CoreContactsError> for ContactsError {
fn from(value: CoreContactsError) -> Self {
match value {
CoreContactsError::Http(error) => {
- let message = error_chain(&error);
+ let message = ente_core::error::chain(&error);
match error {
ente_core::http::Error::Http { status, .. }
| ente_core::http::Error::Api { status, .. } => {
@@ -96,16 +95,6 @@ impl From<CoreContactsError> for ContactsError {
}
}
-fn error_chain(error: &dyn std::error::Error) -> String {
- let mut message = error.to_string();
- let mut source = error.source();
- while let Some(cause) = source {
- let _ = write!(message, ": {cause}");
- source = cause.source();
- }
- message
-}
-
#[frb]
#[derive(Clone)]
/// Persistable wrapped root contact key returned from Rust.
diff --git a/rust/bindings/uniffi/cast/src/lib.rs b/rust/bindings/uniffi/cast/src/lib.rs
index 294c0e7a19..30966ec089 100644
--- a/rust/bindings/uniffi/cast/src/lib.rs
+++ b/rust/bindings/uniffi/cast/src/lib.rs
@@ -11,7 +11,7 @@ pub enum CastCryptoError {
impl From<crypto::Error> for CastCryptoError {
fn from(err: crypto::Error) -> Self {
- CastCryptoError::Message(err.to_string())
+ CastCryptoError::Message(ente_core::error::chain(&err))
}
}
diff --git a/rust/bindings/wasm/ente-wasm/src/auth.rs b/rust/bindings/wasm/ente-wasm/src/auth.rs
index e24155306f..d482d80666 100644
--- a/rust/bindings/wasm/ente-wasm/src/auth.rs
+++ b/rust/bindings/wasm/ente-wasm/src/auth.rs
@@ -45,7 +45,7 @@ impl From<core_auth::AuthError> for AuthError {
Self {
code,
- message: e.to_string(),
+ message: ente_core::error::chain(&e),
}
}
}
diff --git a/rust/bindings/wasm/ente-wasm/src/contacts.rs b/rust/bindings/wasm/ente-wasm/src/contacts.rs
index 6006638020..41c1aaf03e 100644
--- a/rust/bindings/wasm/ente-wasm/src/contacts.rs
+++ b/rust/bindings/wasm/ente-wasm/src/contacts.rs
@@ -48,7 +48,7 @@ impl From<CoreContactsError> for ContactsError {
Self {
code,
- message: e.to_string(),
+ message: ente_core::error::chain(&e),
}
}
}
diff --git a/rust/bindings/wasm/ente-wasm/src/crypto.rs b/rust/bindings/wasm/ente-wasm/src/crypto.rs
index a21ddd2fc9..46e36d88d5 100644
--- a/rust/bindings/wasm/ente-wasm/src/crypto.rs
+++ b/rust/bindings/wasm/ente-wasm/src/crypto.rs
@@ -59,7 +59,7 @@ impl From<core_crypto::Error> for CryptoError {
Self {
code,
- message: e.to_string(),
+ message: ente_core::error::chain(&e),
}
}
}
diff --git a/rust/crates/core/src/error.rs b/rust/crates/core/src/error.rs
new file mode 100644
index 0000000000..28c940bc12
--- /dev/null
+++ b/rust/crates/core/src/error.rs
@@ -0,0 +1,53 @@
+//! Helpers for working with errors.
+
+use std::fmt::Write;
+
+/// Render an error and its chain of causes as one string.
+///
+/// The causes are appended, colon separated, to the error's own message.
+pub fn chain(error: &dyn std::error::Error) -> String {
+ let mut message = error.to_string();
+ let mut source = error.source();
+ while let Some(cause) = source {
+ let _ = write!(message, ": {cause}");
+ source = cause.source();
+ }
+ message
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[derive(Debug)]
+ struct Leaf;
+
+ impl std::fmt::Display for Leaf {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "leaf")
+ }
+ }
+
+ impl std::error::Error for Leaf {}
+
+ #[derive(Debug)]
+ struct Wrapper(Leaf);
+
+ impl std::fmt::Display for Wrapper {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "wrapper")
+ }
+ }
+
+ impl std::error::Error for Wrapper {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ Some(&self.0)
+ }
+ }
+
+ #[test]
+ fn chain_renders_the_causes() {
+ assert_eq!(chain(&Leaf), "leaf");
+ assert_eq!(chain(&Wrapper(Leaf)), "wrapper: leaf");
+ }
+}
diff --git a/rust/crates/core/src/lib.rs b/rust/crates/core/src/lib.rs
index fded1318de..5f9af3d4a1 100644
--- a/rust/crates/core/src/lib.rs
+++ b/rust/crates/core/src/lib.rs
@@ -5,6 +5,7 @@
pub mod auth;
pub mod crypto;
+pub mod error;
pub mod http;
pub mod http_legacy;
pub mod io;