Skip to content

Commit

Permalink
refactor: improve error handling
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ShenMian committed Dec 10, 2024
1 parent 82b3197 commit 9d6c45b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event> {
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")
)
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 10 additions & 5 deletions src/satellite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub enum Satellite {
impl Satellite {
pub fn get_elements(&self) -> Option<Vec<sgp4::Elements>> {
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();

Expand All @@ -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();
}
Expand Down Expand Up @@ -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()
}
}
7 changes: 4 additions & 3 deletions src/widgets/object_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 9d6c45b

Please sign in to comment.