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

index 49668d0..43ecb4a 100644 (file)
@@ -3,6 +3,7 @@
  */
 
 use std::{
+    fmt,
     net::UdpSocket,
     sync::{Arc, Mutex},
     thread,
@@ -59,8 +60,8 @@ fn main() -> Result<()> {
                 // Send e-mail.
                 println!("Sending email...");
                 match send_email(
-                    "Staking ERROR",
-                    &format!("Error: {:?}", error),
+                    "Watchdog: Staking error",
+                    &format!("Error: {}", error),
                     &config.smtp_login,
                     &config.smtp_password,
                 ) {
@@ -105,6 +106,58 @@ enum CheckError {
     CheckAlive,
 }
 
+impl fmt::Display for CheckError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            CheckError::HttpError(text) => {
+                write!(
+                    f,
+                    "Beacon node health check can't be reached (HTTP error): {}",
+                    text
+                )
+            }
+            CheckError::NotSync => {
+                write!(
+                    f,
+                    "Beacon node health check is syncing (currently not sync)"
+                )
+            }
+            CheckError::InvalidSyncStatus => {
+                write!(f, "Beacon node health check returns an invalid status code")
+            }
+            CheckError::NodeHavingIssues => {
+                write!(
+                    f,
+                    "Beacon node health check is not initilized or having issue"
+                )
+            }
+            CheckError::UnknownCodeFromHealthCheck(code) => {
+                write!(
+                    f,
+                    "Beacon node health check returns an unknown code: {}",
+                    code
+                )
+            }
+            CheckError::ReqwestError(error) => {
+                write!(f, "Error from reqwest: {}", error)
+            }
+            CheckError::ValidatorError { pub_key, message } => {
+                write!(f, "Validator '{}' returns an error: {}", pub_key, message)
+            }
+            CheckError::ValidatorStatusError { pub_key, message } => {
+                write!(
+                    f,
+                    "Validator '{}' returns a status error: {}",
+                    pub_key, message
+                )
+            }
+            CheckError::CheckAlive => {
+                write!(f, "Check alive ping hasn't been received")
+            }
+        }
+    }
+}
+
 impl From<reqwest::Error> for CheckError {
     fn from(value: reqwest::Error) -> Self {
         CheckError::ReqwestError(value)
@@ -204,7 +257,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();