Better error formatting
authorGreg Burri <greg.burri@gmail.com>
Wed, 22 Nov 2023 14:35:01 +0000 (15:35 +0100)
committerGreg Burri <greg.burri@gmail.com>
Wed, 22 Nov 2023 14:35:01 +0000 (15:35 +0100)
src/main.rs

index c854c74..494737e 100644 (file)
@@ -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<Duration, PingError> {
     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();