mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Add CustomErrorHandler and use it
Ignore benign 'Bad Request: message to be replied not found' Telegram error while logging other errors
This commit is contained in:
46
src/bots_manager/custom_error_handler.rs
Normal file
46
src/bots_manager/custom_error_handler.rs
Normal 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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
|
use super::custom_error_handler::CustomErrorHandler;
|
||||||
use teloxide::adaptors::throttle::Limits;
|
use teloxide::adaptors::throttle::Limits;
|
||||||
use teloxide::dispatching::Dispatcher;
|
use teloxide::dispatching::Dispatcher;
|
||||||
use teloxide::error_handlers::LoggingErrorHandler;
|
|
||||||
use teloxide::requests::{Request, Requester, RequesterExt};
|
use teloxide::requests::{Request, Requester, RequesterExt};
|
||||||
use teloxide::stop::StopToken;
|
use teloxide::stop::StopToken;
|
||||||
use teloxide::stop::{mk_stop_token, StopFlag};
|
use teloxide::stop::{mk_stop_token, StopFlag};
|
||||||
@@ -108,7 +109,7 @@ pub async fn start_bot(bot_data: &BotData) {
|
|||||||
dispatcher
|
dispatcher
|
||||||
.dispatch_with_listener(
|
.dispatch_with_listener(
|
||||||
listener,
|
listener,
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
CustomErrorHandler::with_custom_text("An error from the update listener"),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
pub mod axum_server;
|
pub mod axum_server;
|
||||||
pub mod bot_manager_client;
|
pub mod bot_manager_client;
|
||||||
pub mod closable_sender;
|
pub mod closable_sender;
|
||||||
|
pub mod custom_error_handler;
|
||||||
pub mod internal;
|
pub mod internal;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user