From 6cbb07bc2dabc69c4d27000f091ef5e9c334841d Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 20 Sep 2021 12:53:12 +0200 Subject: [PATCH 1/3] Fix broken anyhow feature --- crates/matrix-sdk/README.md | 3 ++- crates/matrix-sdk/src/event_handler.rs | 32 +++++++++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/crates/matrix-sdk/README.md b/crates/matrix-sdk/README.md index 7477a9f54..3911764d7 100644 --- a/crates/matrix-sdk/README.md +++ b/crates/matrix-sdk/README.md @@ -60,7 +60,8 @@ The following crate feature flags are available: `require_auth_for_profile_requests`. Enabled by default. * `appservice`: Enables low-level appservice functionality. For an high-level API there's the `matrix-sdk-appservice` crate -* `anyhow`: Support for returning `anyhow::Result<()>` from event handlers. +* `anyhow`: More verbose error logging for event handlers that return + `anyhow::Result<()>`. # Enabling logging diff --git a/crates/matrix-sdk/src/event_handler.rs b/crates/matrix-sdk/src/event_handler.rs index 559015362..4834bd9bd 100644 --- a/crates/matrix-sdk/src/event_handler.rs +++ b/crates/matrix-sdk/src/event_handler.rs @@ -30,7 +30,9 @@ //! //! For more details, see the [`EventHandler`] trait. -use std::{borrow::Cow, future::Future, ops::Deref}; +#[cfg(feature = "anyhow")] +use std::any::TypeId; +use std::{borrow::Cow, fmt, future::Future, ops::Deref}; use matrix_sdk_base::deserialized_responses::{EncryptionInfo, SyncRoomEvent}; use ruma::{events::AnySyncStateEvent, serde::Raw}; @@ -67,9 +69,9 @@ pub trait SyncEvent { /// * They must have at least one argument, which is the event itself, a type /// that implements [`SyncEvent`]. Any additional arguments need to implement /// the [`EventHandlerContext`] trait. -/// * Their return type has to be one of: `()`, `Result<(), impl -/// std::error::Error>` or `anyhow::Result<()>` (requires the `anyhow` Cargo -/// feature to be enabled) +/// * Their return type has to be one of: `()`, `Result<(), impl Display + Debug +/// + 'static>` (if you are using `anyhow::Result` you can additionally enable +/// the `anyhow` feature to get the verbose `Debug` output printed on error) /// /// ### How it works /// @@ -188,19 +190,17 @@ impl EventHandlerResult for () { fn print_error(&self, _event_type: &str) {} } -impl EventHandlerResult for Result<(), E> { +impl EventHandlerResult for Result<(), E> { fn print_error(&self, event_type: &str) { - if let Err(e) = self { - tracing::error!("Event handler for `{}` failed: {}", event_type, e); - } - } -} - -#[cfg(feature = "anyhow")] -impl EventHandlerResult for anyhow::Result<()> { - fn print_error(&self, event_type: &str) { - if let Err(e) = self { - tracing::error!("Event handler for `{}` failed: {:?}", event_type, e); + match self { + #[cfg(feature = "anyhow")] + Err(e) if TypeId::of::() == TypeId::of::() => { + tracing::error!("Event handler for `{}` failed: {:?}", event_type, e); + } + Err(e) => { + tracing::error!("Event handler for `{}` failed: {}", event_type, e); + } + Ok(_) => {} } } } From f752bded4b126ba4185c31ac65614c36bd73fe11 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 20 Sep 2021 12:56:40 +0200 Subject: [PATCH 2/3] Add eyre feature to matrix-sdk --- crates/matrix-sdk/Cargo.toml | 1 + crates/matrix-sdk/README.md | 2 ++ crates/matrix-sdk/src/event_handler.rs | 11 ++++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/matrix-sdk/Cargo.toml b/crates/matrix-sdk/Cargo.toml index 4a03b138e..ee8b93c10 100644 --- a/crates/matrix-sdk/Cargo.toml +++ b/crates/matrix-sdk/Cargo.toml @@ -38,6 +38,7 @@ anyhow = { version = "1.0.42", optional = true } bytes = "1.0.1" dashmap = "4.0.2" event-listener = "2.5.1" +eyre = { version = "0.6.5", optional = true } futures = "0.3.15" http = "0.2.4" matrix-sdk-common = { version = "0.4.0", path = "../matrix-sdk-common" } diff --git a/crates/matrix-sdk/README.md b/crates/matrix-sdk/README.md index 3911764d7..bc9c23c3b 100644 --- a/crates/matrix-sdk/README.md +++ b/crates/matrix-sdk/README.md @@ -62,6 +62,8 @@ The following crate feature flags are available: high-level API there's the `matrix-sdk-appservice` crate * `anyhow`: More verbose error logging for event handlers that return `anyhow::Result<()>`. +* `eyre`: More verbose error logging for event handlers that return + `eyre::Result<()>`. # Enabling logging diff --git a/crates/matrix-sdk/src/event_handler.rs b/crates/matrix-sdk/src/event_handler.rs index 4834bd9bd..016030cd3 100644 --- a/crates/matrix-sdk/src/event_handler.rs +++ b/crates/matrix-sdk/src/event_handler.rs @@ -30,7 +30,7 @@ //! //! For more details, see the [`EventHandler`] trait. -#[cfg(feature = "anyhow")] +#[cfg(any(feature = "anyhow", feature = "eyre"))] use std::any::TypeId; use std::{borrow::Cow, fmt, future::Future, ops::Deref}; @@ -70,8 +70,9 @@ pub trait SyncEvent { /// that implements [`SyncEvent`]. Any additional arguments need to implement /// the [`EventHandlerContext`] trait. /// * Their return type has to be one of: `()`, `Result<(), impl Display + Debug -/// + 'static>` (if you are using `anyhow::Result` you can additionally enable -/// the `anyhow` feature to get the verbose `Debug` output printed on error) +/// + 'static>` (if you are using `anyhow::Result` or `eyre::Result` you can +/// additionally enable the `anyhow` / `eyre` feature to get the verbose +/// `Debug` output printed on error) /// /// ### How it works /// @@ -197,6 +198,10 @@ impl EventHandlerResult for Result<(), E Err(e) if TypeId::of::() == TypeId::of::() => { tracing::error!("Event handler for `{}` failed: {:?}", event_type, e); } + #[cfg(feature = "eyre")] + Err(e) if TypeId::of::() == TypeId::of::() => { + tracing::error!("Event handler for `{}` failed: {:?}", event_type, e); + } Err(e) => { tracing::error!("Event handler for `{}` failed: {}", event_type, e); } From cbc073904153cb9d683e6b37cf70dc9fc2c390df Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 20 Sep 2021 21:21:46 +0200 Subject: [PATCH 3/3] Re-export anyhow and eyre features from matrix-sdk-appservice --- crates/matrix-sdk-appservice/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/matrix-sdk-appservice/Cargo.toml b/crates/matrix-sdk-appservice/Cargo.toml index babf32e58..0e49409c8 100644 --- a/crates/matrix-sdk-appservice/Cargo.toml +++ b/crates/matrix-sdk-appservice/Cargo.toml @@ -10,7 +10,9 @@ version = "0.1.0" [features] default = ["warp", "native-tls"] +anyhow = ["matrix-sdk/anyhow"] encryption = ["matrix-sdk/encryption"] +eyre = ["matrix-sdk/eyre"] sled_state_store = ["matrix-sdk/sled_state_store"] sled_cryptostore = ["matrix-sdk/sled_cryptostore"] markdown = ["matrix-sdk/markdown"]