diff --git a/src/bots_manager/custom_error_handler.rs b/src/bots_manager/custom_error_handler.rs new file mode 100644 index 0000000..a9c5415 --- /dev/null +++ b/src/bots_manager/custom_error_handler.rs @@ -0,0 +1,46 @@ +use std::future::Future; +use std::pin::Pin; +use std::sync::Arc; +use tracing::log; + +pub struct CustomErrorHandler { + pub text: String, +} + +impl CustomErrorHandler { + pub fn with_custom_text(text: T) -> Arc + where + T: Into, + { + Arc::new(Self { text: text.into() }) + } +} + +impl teloxide::error_handlers::ErrorHandler for CustomErrorHandler +where + E: std::fmt::Debug + Send + 'static, +{ + fn handle_error( + self: Arc, + error: E, + ) -> Pin + Send + 'static>> { + Box::pin(async move { + let error_string = format!("{:?}", error); + + if error_string.contains("Bad Request: message to be replied not found") { + log::debug!("Ignoring Telegram reply error: {:?}", error); + return; + } + + log::error!("{}: {:?}", self.text, error); + }) + } +} + +impl Default for CustomErrorHandler { + fn default() -> Self { + Self { + text: "An error from the update listener".to_string(), + } + } +} diff --git a/src/bots_manager/internal.rs b/src/bots_manager/internal.rs index 9d8640d..837932c 100644 --- a/src/bots_manager/internal.rs +++ b/src/bots_manager/internal.rs @@ -1,6 +1,7 @@ +use super::custom_error_handler::CustomErrorHandler; use teloxide::adaptors::throttle::Limits; use teloxide::dispatching::Dispatcher; -use teloxide::error_handlers::LoggingErrorHandler; + use teloxide::requests::{Request, Requester, RequesterExt}; use teloxide::stop::StopToken; use teloxide::stop::{mk_stop_token, StopFlag}; @@ -108,7 +109,7 @@ pub async fn start_bot(bot_data: &BotData) { dispatcher .dispatch_with_listener( listener, - LoggingErrorHandler::with_custom_text("An error from the update listener"), + CustomErrorHandler::with_custom_text("An error from the update listener"), ) .await; }); diff --git a/src/bots_manager/mod.rs b/src/bots_manager/mod.rs index 647d0fc..259d90c 100644 --- a/src/bots_manager/mod.rs +++ b/src/bots_manager/mod.rs @@ -1,6 +1,7 @@ pub mod axum_server; pub mod bot_manager_client; pub mod closable_sender; +pub mod custom_error_handler; pub mod internal; pub mod utils;