diff --git a/src/dispatching/error_handler.rs b/src/dispatching/error_handler.rs index c663b054..82a8f344 100644 --- a/src/dispatching/error_handler.rs +++ b/src/dispatching/error_handler.rs @@ -4,7 +4,7 @@ //! should be prettier. // Infallible used here instead of `!` to be compatible with rust <1.41. -use std::{convert::Infallible, future::Future, pin::Pin}; +use std::{convert::Infallible, fmt::Debug, future::Future, pin::Pin}; /// An asynchronous handler of an error. pub trait ErrorHandler { @@ -21,7 +21,7 @@ pub trait ErrorHandler { /// ## Example /// ``` /// # #[tokio::main] -/// # async fn main_() { +/// # async fn main() { /// use teloxide::dispatching::error_handler::{ErrorHandler, Ignore}; /// /// Ignore.handle_error(()).await; @@ -50,7 +50,7 @@ impl ErrorHandler for Ignore { /// ## Examples /// ``` /// # #[tokio::main] -/// # async fn main_() { +/// # async fn main() { /// use std::convert::{Infallible, TryInto}; /// /// use teloxide::dispatching::error_handler::{ErrorHandler, IgnoreSafe}; @@ -90,6 +90,37 @@ impl ErrorHandler for IgnoreSafe { } } +/// An error handler that prints all errors passed into it. +/// +/// ## Example +/// ``` +/// # #[tokio::main] +/// # async fn main() { +/// use teloxide::dispatching::error_handler::{ErrorHandler, Print}; +/// +/// Print.handle_error(()).await; +/// Print.handle_error(404).await; +/// Print.handle_error(String::from("error")).await; +/// # } +/// ``` +pub struct Print; + +impl ErrorHandler for Print +where + E: Debug, +{ + fn handle_error<'a>( + &'a self, + error: E, + ) -> Pin + 'a>> + where + E: 'a, + { + log::debug!("error: {:?}", error); + Box::pin(async {}) + } +} + /// The implementation of `ErrorHandler` for `Fn(error) -> Future`. /// /// ## Example