Browse Source
In general, we would like to let the sender of an `xtra::Message` to be able to handle all the possible errors. The `log_error` macro catered to the complete opposite use case, so we remove it. In order to make error handling at the sending site more ergonomic, we introduce a `LogFailure` trait designed for fallible `xtra::Messages`. With this patch we've tried to change _where_ we handle errors, but not _what_ we do with them.update-blockstream-electrum-server-url
15 changed files with 267 additions and 165 deletions
@ -1,9 +0,0 @@ |
|||
/// Wrapper for handlers to log errors
|
|||
#[macro_export] |
|||
macro_rules! log_error { |
|||
($future:expr) => { |
|||
if let Err(e) = $future.await { |
|||
tracing::error!("Message handler failed: {:#}", e); |
|||
} |
|||
}; |
|||
} |
@ -0,0 +1,50 @@ |
|||
use async_trait::async_trait; |
|||
use xtra::address; |
|||
use xtra::message_channel; |
|||
use xtra::Actor; |
|||
use xtra::Disconnected; |
|||
use xtra::Message; |
|||
|
|||
#[async_trait] |
|||
pub trait LogFailure { |
|||
async fn log_failure(self, context: &str) -> Result<(), Disconnected>; |
|||
} |
|||
|
|||
#[async_trait] |
|||
impl<A, M> LogFailure for address::SendFuture<A, M> |
|||
where |
|||
A: Actor, |
|||
M: Message<Result = anyhow::Result<()>>, |
|||
{ |
|||
async fn log_failure(self, context: &str) -> Result<(), Disconnected> { |
|||
if let Err(e) = self.await? { |
|||
tracing::warn!( |
|||
"{}: Message handler for message {} failed: {:#}", |
|||
context, |
|||
std::any::type_name::<M>(), |
|||
e |
|||
); |
|||
} |
|||
|
|||
Ok(()) |
|||
} |
|||
} |
|||
|
|||
#[async_trait] |
|||
impl<M> LogFailure for message_channel::SendFuture<M> |
|||
where |
|||
M: xtra::Message<Result = anyhow::Result<()>>, |
|||
{ |
|||
async fn log_failure(self, context: &str) -> Result<(), Disconnected> { |
|||
if let Err(e) = self.await? { |
|||
tracing::warn!( |
|||
"{}: Message handler for message {} failed: {:#}", |
|||
context, |
|||
std::any::type_name::<M>(), |
|||
e |
|||
); |
|||
} |
|||
|
|||
Ok(()) |
|||
} |
|||
} |
Loading…
Reference in new issue