From 41c060d7d00e07bc04c293f0b0e8b5f5d989c4d5 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 25 Aug 2024 12:57:33 +0200 Subject: [PATCH] Improve error messages for custom parsing failures --- src/services/telegram/commands/diario.rs | 2 +- src/services/telegram/commands/mod.rs | 4 ++-- src/services/telegram/mod.rs | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/telegram/commands/diario.rs b/src/services/telegram/commands/diario.rs index 10e88585..340722ce 100644 --- a/src/services/telegram/commands/diario.rs +++ b/src/services/telegram/commands/diario.rs @@ -50,7 +50,7 @@ impl FromStr for DiarioArgs { DiarioArgs { warning, quote, quoted, context } } None => { - anyhow::ensure!(!s.is_empty(), "La citazione specificata non deve essere vuota."); + anyhow::ensure!(!s.is_empty(), "la citazione non deve essere vuota."); let warning = None; let quote = s.to_string(); diff --git a/src/services/telegram/commands/mod.rs b/src/services/telegram/commands/mod.rs index 36f8dcdb..3ffd1144 100644 --- a/src/services/telegram/commands/mod.rs +++ b/src/services/telegram/commands/mod.rs @@ -151,11 +151,11 @@ impl Command { Ok(()) } - pub async fn handle_malformed_complex(bot: Bot, message: Message) -> CommandResult { + pub async fn handle_malformed_complex(bot: &Bot, message: &Message, error: &Error) -> CommandResult { log::debug!("Received a malformed command: {:?}", message.text()); log::trace!("Sending error message..."); - let text = "⚠️ Il comando si aspetta una sintassi diversa da quella che ha ricevuto."; + let text = format!("⚠️ Sintassi del comando errata: {error}"); let _reply = bot .send_message(message.chat.id, text) .reply_parameters(ReplyParameters::new(message.id)) diff --git a/src/services/telegram/mod.rs b/src/services/telegram/mod.rs index a5b57928..55286b0f 100644 --- a/src/services/telegram/mod.rs +++ b/src/services/telegram/mod.rs @@ -138,7 +138,8 @@ impl TelegramService { } Err(ParseError::IncorrectFormat(e)) => { log::debug!("Message text is a command with a custom format, but the parser returned the error {e:?}; handling as a malformed command."); - Command::handle_malformed_complex(bot, message).await + let error = anyhow::format_err!(e); + Command::handle_malformed_complex(&bot, &message, &error).await .context("Impossibile gestire comando malformato complesso.")?; return Ok(()); }