refactor(app): better error handling
This commit is contained in:
@@ -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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_all_metrics_notification(&self) {
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user