refactor(app): better error handling
This commit is contained in:
@@ -21,22 +21,22 @@ pub fn new(
|
||||
}
|
||||
|
||||
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
|
||||
let violations = self.monitor.check_thresholds();
|
||||
for violation in violations {
|
||||
let message = self.formatter.format_violation(violation);
|
||||
self.notifier
|
||||
.send_notification(&message)
|
||||
.expect("Failed to send notification");
|
||||
self.notifier.send_notification(&message)?;
|
||||
}
|
||||
|
||||
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
|
||||
let summary = self.formatter.format_summary(&self.monitor.get_metrics());
|
||||
self.notifier
|
||||
.send_notification(&summary)
|
||||
.expect("Failed to send final notification");
|
||||
self.notifier.send_notification(&summary)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -45,6 +45,12 @@ fn main() {
|
||||
Box::new(system_monitor_adapter),
|
||||
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