feat: add notifications when errors

This commit is contained in:
trafficlunar 2024-11-29 15:27:58 +00:00
parent 8950e16e70
commit ce32c1a67e
5 changed files with 1079 additions and 1 deletions

View file

@ -1,6 +1,7 @@
use std::error::Error;
mod websocket;
mod notifications;
fn main() -> Result<(), Box<dyn Error>> {
dotenvy::dotenv()?;

11
src/notifications.rs Normal file
View file

@ -0,0 +1,11 @@
use notify_rust::Notification;
pub fn send_error_notification(body: &str) {
Notification::new()
.summary("Computer Statistics Error")
.body(body)
.icon("dialog-error")
.timeout(0)
.show()
.unwrap();
}

View file

@ -2,11 +2,15 @@ use std::{env, net::TcpStream};
use tungstenite::{stream::MaybeTlsStream, Message, WebSocket};
use crate::notifications;
pub fn connect() -> Result<WebSocket<MaybeTlsStream<TcpStream>>, tungstenite::Error> {
let (socket, _) = match tungstenite::connect(env::var("WEBSOCKET_URL").unwrap()) {
Ok(ws) => ws,
Err(err) => {
eprintln!("Unable to connect to WebSocket");
eprintln!("Unable to connect to WebSocket: {}", err);
notifications::send_error_notification("Unable to connect to WebSocket");
return Err(err);
}
};