Add CustomErrorHandler and use it
Some checks are pending
Build docker image / Build-Docker-Image (push) Waiting to run
rust-clippy analyze / Run rust-clippy analyzing (push) Waiting to run

Ignore benign 'Bad Request: message to be replied not found' Telegram
error while logging other errors
This commit is contained in:
2025-10-25 20:16:19 +02:00
parent 17ef8a7f3d
commit 359a6b6137
3 changed files with 50 additions and 2 deletions

View File

@@ -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<T>(text: T) -> Arc<Self>
where
T: Into<String>,
{
Arc::new(Self { text: text.into() })
}
}
impl<E> teloxide::error_handlers::ErrorHandler<E> for CustomErrorHandler
where
E: std::fmt::Debug + Send + 'static,
{
fn handle_error(
self: Arc<Self>,
error: E,
) -> Pin<Box<dyn Future<Output = ()> + 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(),
}
}
}

View File

@@ -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;
});

View File

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