From: Greg Burri Date: Wed, 22 Nov 2023 14:35:01 +0000 (+0100) Subject: Better error formatting X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=cf8d83bfd816ea2aa2c10265451190e072a303d7;p=stakingWatchdogWatchdog.git Better error formatting --- diff --git a/src/main.rs b/src/main.rs index c854c74..494737e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::{ + fmt, net::UdpSocket, thread, time::{self, Duration}, @@ -79,8 +80,8 @@ fn main() -> Result<()> { // Send e-mail. println!("Sending email..."); match send_email( - "Watchdog Watchdog ERROR", - &format!("Error: {:?}", error), + "Watchdog Watchdog: Check alive error", + &format!("Error: {}", error), &config.smtp_login, &config.smtp_password, ) { @@ -110,6 +111,22 @@ enum PingError { WrongMessageReceived(String), } +impl fmt::Display for PingError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + PingError::SocketReceiveError(error) => { + write!(f, "Didn't receive any response from watchdog: {}", error) + } + PingError::SocketSendError(error) => { + write!(f, "Unable to send the message: {}", error) + } + PingError::WrongMessageReceived(message) => { + write!(f, "Watchdog replay with a wrong message: {}", message) + } + } + } +} + fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result { loop { let number: u64 = rng.gen(); @@ -164,7 +181,7 @@ fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> { let creds = Credentials::new(login.to_string(), pass.to_string()); // Open a remote connection to gmail - let mailer = SmtpTransport::relay("mail.gandi.net")? + let mailer = SmtpTransport::relay("mail.infomaniak.com")? .credentials(creds) .build();