diff --git a/crates/matrix-sdk-appservice/Cargo.toml b/crates/matrix-sdk-appservice/Cargo.toml index 621c3ab06..58220369f 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"] diff --git a/crates/matrix-sdk/Cargo.toml b/crates/matrix-sdk/Cargo.toml index 8f6247e6c..588f5824e 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 7477a9f54..bc9c23c3b 100644 --- a/crates/matrix-sdk/README.md +++ b/crates/matrix-sdk/README.md @@ -60,7 +60,10 @@ 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<()>`. +* `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 559015362..016030cd3 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(any(feature = "anyhow", feature = "eyre"))] +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,10 @@ 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` or `eyre::Result` you can +/// additionally enable the `anyhow` / `eyre` feature to get the verbose +/// `Debug` output printed on error) /// /// ### How it works /// @@ -188,19 +191,21 @@ 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); + } + #[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); + } + Ok(_) => {} } } }