From 19671d1006f19d4a9fbdb1800ffb9e992fd3976d Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 22 Nov 2023 15:30:17 +0100 Subject: [PATCH] Better error formatting --- src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 49668d0..43ecb4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 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(); -- 2.45.2