From 9d6c45b6dc226b71d8b2542b464ac95f7d8252a9 Mon Sep 17 00:00:00 2001 From: ShenMian Date: Tue, 10 Dec 2024 15:30:38 +0000 Subject: [PATCH] refactor: improve error handling - Update error handling in event receiver - Improve Object creation from elements with default values - Enhance Satellite enum to handle cache expiration - Refactor Celestrak request for better error handling - Optimize clipboard operations in object information widget --- src/event.rs | 7 +++---- src/object.rs | 12 ++++++++++-- src/satellite.rs | 15 ++++++++++----- src/widgets/object_information.rs | 7 ++++--- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/event.rs b/src/event.rs index f07330d..3f66c85 100644 --- a/src/event.rs +++ b/src/event.rs @@ -80,10 +80,9 @@ impl EventHandler { /// This function will always block the current thread if /// there is no data available and it's possible for more data to be sent. pub async fn next(&mut self) -> Result { - self.receiver - .recv() - .await - .ok_or(std::io::Error::new(std::io::ErrorKind::Other, "This is an IO error").into()) + self.receiver.recv().await.ok_or( + anyhow::anyhow!("the event receiver has been closed and there are no remaining messages in the receiver's buffer") + ) } } diff --git a/src/object.rs b/src/object.rs index a883046..c1dbda0 100644 --- a/src/object.rs +++ b/src/object.rs @@ -25,8 +25,16 @@ pub struct Object { impl Object { pub fn from_elements(elements: sgp4::Elements) -> Self { Self { - name: elements.object_name.as_ref().unwrap().clone(), - cospar_id: elements.international_designator.as_ref().unwrap().clone(), + name: elements + .object_name + .as_ref() + .cloned() + .unwrap_or("Unknown".to_string()), + cospar_id: elements + .international_designator + .as_ref() + .cloned() + .unwrap_or("Unknown".to_string()), norad_id: elements.norad_id, epoch: DateTime::from_naive_utc_and_offset(elements.datetime, Utc), drag_term: elements.drag_term, diff --git a/src/satellite.rs b/src/satellite.rs index 79e4fc5..e4a78a1 100644 --- a/src/satellite.rs +++ b/src/satellite.rs @@ -53,7 +53,7 @@ pub enum Satellite { impl Satellite { pub fn get_elements(&self) -> Option> { let cache_path = dirs::cache_dir() - .unwrap() + .expect("failed to get cache directory") .join(format!("tracker/{}.json", self.to_string().to_lowercase())); fs::create_dir_all(cache_path.parent().unwrap()).unwrap(); @@ -72,9 +72,10 @@ impl Satellite { .unwrap() .elapsed() .unwrap(); + let is_cache_expired = age > Duration::from_secs(2 * 60 * 60); // Fetch elements if cache is older than 2 hours - if age > Duration::from_secs(2 * 60 * 60) { + if is_cache_expired { if let Some(elements) = self.fetch_elements() { fs::write(&cache_path, serde_json::to_string(&elements).unwrap()).unwrap(); } @@ -123,14 +124,18 @@ impl Satellite { ureq::get("https://celestrak.org/NORAD/elements/gp.php").query("FORMAT", "json"); request = match (self.cospar_id(), self.group()) { - (Some(id), _) => request.query("INTDES", id), + (Some(id), None) => request.query("INTDES", id), (None, Some(group)) => request.query("GROUP", group), - (None, None) => unreachable!(), + _ => unreachable!(), }; request .call() - .map(|response| response.into_json().unwrap()) + .map(|response| { + response + .into_json() + .expect("failed to parse JSON from celestrak.org") + }) .ok() } } diff --git a/src/widgets/object_information.rs b/src/widgets/object_information.rs index 5ca28fe..47d71a5 100644 --- a/src/widgets/object_information.rs +++ b/src/widgets/object_information.rs @@ -176,9 +176,10 @@ pub async fn handle_mouse_events(event: MouseEvent, app: &mut App) -> Result<()> MouseEventKind::Down(MouseButton::Left) => { // Copy the clicked value to the clipboard. if let Some(index) = app.object_information_state.table_state.selected() { - let mut clipboard = Clipboard::new().unwrap(); - let value = app.object_information_state.items[index].1.clone(); - clipboard.set_text(value).unwrap(); + if let Ok(mut clipboard) = Clipboard::new() { + let value = app.object_information_state.items[index].1.clone(); + clipboard.set_text(value).unwrap(); + } } } MouseEventKind::ScrollDown => {