refactor(app): better error handling

This commit is contained in:
2025-08-09 22:42:30 +02:00
parent f3e8b01d7e
commit 12cf26d8a6
2 changed files with 16 additions and 10 deletions

View File

@@ -21,22 +21,22 @@ pub fn new(
} }
impl Service { impl Service {
pub fn alert_on_threshold_violation(&self) { pub fn alert_on_threshold_violation(&self) -> Result<(), Box<dyn std::error::Error>> {
// Check for threshold violations and send alerts // Check for threshold violations and send alerts
let violations = self.monitor.check_thresholds(); let violations = self.monitor.check_thresholds();
for violation in violations { for violation in violations {
let message = self.formatter.format_violation(violation); let message = self.formatter.format_violation(violation);
self.notifier self.notifier.send_notification(&message)?;
.send_notification(&message)
.expect("Failed to send notification");
} }
Ok(())
} }
pub fn send_all_metrics_notification(&self) { pub fn send_all_metrics_notification(&self) -> Result<(), Box<dyn std::error::Error>> {
// Send a final notification with all system information // Send a final notification with all system information
let summary = self.formatter.format_summary(&self.monitor.get_metrics()); let summary = self.formatter.format_summary(&self.monitor.get_metrics());
self.notifier self.notifier.send_notification(&summary)?;
.send_notification(&summary)
.expect("Failed to send final notification"); Ok(())
} }
} }

View File

@@ -45,6 +45,12 @@ fn main() {
Box::new(system_monitor_adapter), Box::new(system_monitor_adapter),
Box::new(formatter), Box::new(formatter),
); );
service.alert_on_threshold_violation();
service.send_all_metrics_notification(); if let Err(e) = service.alert_on_threshold_violation() {
eprintln!("Error during threshold violation check: {}", e);
}
if let Err(e) = service.send_all_metrics_notification() {
eprintln!("Error sending final notification: {}", e);
}
} }