Merge branch 'fix-anyhow-feature'

This commit is contained in:
Damir Jelić
2021-09-21 14:54:40 +02:00
4 changed files with 28 additions and 17 deletions

View File

@@ -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"]

View File

@@ -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" }

View File

@@ -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

View File

@@ -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<E: std::error::Error> EventHandlerResult for Result<(), E> {
impl<E: fmt::Debug + fmt::Display + 'static> 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::<E>() == TypeId::of::<anyhow::Error>() => {
tracing::error!("Event handler for `{}` failed: {:?}", event_type, e);
}
#[cfg(feature = "eyre")]
Err(e) if TypeId::of::<E>() == TypeId::of::<eyre::Report>() => {
tracing::error!("Event handler for `{}` failed: {:?}", event_type, e);
}
Err(e) => {
tracing::error!("Event handler for `{}` failed: {}", event_type, e);
}
Ok(_) => {}
}
}
}