mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-31 16:40:37 +01:00
Add error_handler::Print
This commit is contained in:
parent
3c2c1636a4
commit
243b4c9173
1 changed files with 34 additions and 3 deletions
|
@ -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<E> {
|
||||
|
@ -21,7 +21,7 @@ pub trait ErrorHandler<E> {
|
|||
/// ## 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<E> ErrorHandler<E> 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<Infallible> 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<E> ErrorHandler<E> for Print
|
||||
where
|
||||
E: Debug,
|
||||
{
|
||||
fn handle_error<'a>(
|
||||
&'a self,
|
||||
error: E,
|
||||
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
|
||||
where
|
||||
E: 'a,
|
||||
{
|
||||
log::debug!("error: {:?}", error);
|
||||
Box::pin(async {})
|
||||
}
|
||||
}
|
||||
|
||||
/// The implementation of `ErrorHandler` for `Fn(error) -> Future<Output = ()>`.
|
||||
///
|
||||
/// ## Example
|
||||
|
|
Loading…
Reference in a new issue