pub struct LoggingErrorHandler { /* private fields */ }Expand description
A handler that log all errors passed into it.
§Example
use teloxide::error_handlers::{ErrorHandler, LoggingErrorHandler};
LoggingErrorHandler::new().handle_error(()).await;
LoggingErrorHandler::with_custom_text("Omg1").handle_error(404).await;
LoggingErrorHandler::with_custom_text("Omg2").handle_error("Invalid data type!").await;Implementations§
Source§impl LoggingErrorHandler
impl LoggingErrorHandler
Sourcepub fn with_custom_text<T>(text: T) -> Arc<Self> ⓘ
pub fn with_custom_text<T>(text: T) -> Arc<Self> ⓘ
Creates LoggingErrorHandler with a meta text before a log.
The logs will be printed in this format: {text}: {:?}.
Examples found in repository?
examples/dispatching_features.rs (lines 101-103)
12async fn main() {
13 pretty_env_logger::init();
14 log::info!("Starting dispatching features bot...");
15
16 let bot = Bot::from_env();
17
18 let parameters = ConfigParameters {
19 bot_maintainer: UserId(0), // Paste your ID to run this bot.
20 maintainer_username: None,
21 };
22
23 let handler = Update::filter_message()
24 // You can use branching to define multiple ways in which an update will be handled. If the
25 // first branch fails, an update will be passed to the second branch, and so on.
26 .branch(
27 dptree::entry()
28 // Filter commands: the next handlers will receive a parsed `SimpleCommand`.
29 .filter_command::<SimpleCommand>()
30 // If a command parsing fails, this handler will not be executed.
31 .endpoint(simple_commands_handler),
32 )
33 .branch(
34 // Filter a maintainer by a user ID.
35 dptree::filter(|cfg: ConfigParameters, msg: Message| {
36 msg.from.map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
37 })
38 .filter_command::<MaintainerCommands>()
39 .endpoint(|msg: Message, bot: Bot, cmd: MaintainerCommands| async move {
40 match cmd {
41 MaintainerCommands::Rand { from, to } => {
42 let mut rng = rand::rngs::OsRng;
43 let value: u64 = rng.gen_range(from..=to);
44
45 bot.send_message(msg.chat.id, value.to_string()).await?;
46 Ok(())
47 }
48 }
49 }),
50 )
51 .branch(
52 // Filtering allow you to filter updates by some condition.
53 dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
54 .branch(
55 // Filtering by mention allows to filter only `/repeat@my_bot` commands.
56 // Use if you want to make sure that users refer specifically to your bot.
57 // Same as filter_command, the next handlers will receive a parsed
58 // `GroupCommand`.
59 dptree::entry().filter_mention_command::<GroupCommand>().endpoint(
60 |bot: Bot, msg: Message, cmd: GroupCommand| async move {
61 match cmd {
62 GroupCommand::Repeat { text } => {
63 bot.send_message(msg.chat.id, format!("You said: {text}"))
64 .await?;
65 Ok(())
66 }
67 }
68 },
69 ),
70 )
71 .branch(
72 // An endpoint is the last update handler.
73 dptree::endpoint(|msg: Message, bot: Bot| async move {
74 log::info!("Received a message from a group chat.");
75 bot.send_message(msg.chat.id, "This is a group chat.").await?;
76 respond(())
77 }),
78 ),
79 )
80 .branch(
81 // There are some extension filtering functions on `Message`. The following filter will
82 // filter only messages with dices.
83 Message::filter_dice().endpoint(|bot: Bot, msg: Message, dice: Dice| async move {
84 bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
85 .reply_to(msg)
86 .await?;
87 Ok(())
88 }),
89 );
90
91 Dispatcher::builder(bot, handler)
92 // Here you specify initial dependencies that all handlers will receive; they can be
93 // database connections, configurations, and other auxiliary arguments. It is similar to
94 // `actix_web::Extensions`.
95 .dependencies(dptree::deps![parameters])
96 // If no handler succeeded to handle an update, this closure will be called.
97 .default_handler(|upd| async move {
98 log::warn!("Unhandled update: {upd:?}");
99 })
100 // If the dispatcher fails for some reason, execute this handler.
101 .error_handler(LoggingErrorHandler::with_custom_text(
102 "An error has occurred in the dispatcher",
103 ))
104 .enable_ctrlc_handler()
105 .build()
106 .dispatch()
107 .await;
108}Trait Implementations§
Source§impl<E> ErrorHandler<E> for LoggingErrorHandlerwhere
E: Debug,
impl<E> ErrorHandler<E> for LoggingErrorHandlerwhere
E: Debug,
Auto Trait Implementations§
impl Freeze for LoggingErrorHandler
impl RefUnwindSafe for LoggingErrorHandler
impl Send for LoggingErrorHandler
impl Sync for LoggingErrorHandler
impl Unpin for LoggingErrorHandler
impl UnwindSafe for LoggingErrorHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Erasable for T
impl<T> Erasable for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more