Przeglądaj źródła

dnetview: fix bugs in error handling

* only render node_info if our data is not empty
* handle when msgs are Null
* print unexpected data to string
lunar-mining 4 lat temu
rodzic
commit
4edebb22ab
2 zmienionych plików z 22 dodań i 14 usunięć
  1. 1 1
      bin/dnetview/src/error.rs
  2. 21 13
      bin/dnetview/src/view.rs

+ 1 - 1
bin/dnetview/src/error.rs

@@ -10,7 +10,7 @@ pub enum DnetViewError {
     #[error("Failed to find ID at current index")]
     NoIdAtIndex,
     #[error("Found unexpected data in View")]
-    UnexpectedData,
+    UnexpectedData(String),
     #[error("Message log does not contain ID")]
     CannotFindId,
     #[error("ID does not return a selectable object")]

+ 21 - 13
bin/dnetview/src/view.rs

@@ -95,17 +95,22 @@ impl View {
         // remove any duplicates
         id_list.dedup();
 
-        // get the id at the current index
-        match self.active_ids.state.selected() {
-            Some(i) => match id_list.get(i) {
-                Some(i) => {
-                    self.render_info(f, slice.clone(), i.to_string())?;
-                    Ok(())
-                }
-                None => Err(DnetViewError::NoIdAtIndex),
-            },
-            // nothing is selected right now
-            None => Ok(()),
+        if id_list.is_empty() {
+            // we have not received any data
+            Ok(())
+        } else {
+            // get the id at the current index
+            match self.active_ids.state.selected() {
+                Some(i) => match id_list.get(i) {
+                    Some(i) => {
+                        self.render_info(f, slice.clone(), i.to_string())?;
+                        Ok(())
+                    }
+                    None => return Err(DnetViewError::NoIdAtIndex),
+                },
+                // nothing is selected right now
+                None => Ok(()),
+            }
         }
     }
 
@@ -150,7 +155,10 @@ impl View {
                                 );
                                 info.push(msg);
                             }
-                            _ => return Err(DnetViewError::UnexpectedData),
+                            "Null" => {
+                                // Empty msg log. Do nothing
+                            }
+                            data => return Err(DnetViewError::UnexpectedData(data.to_string())),
                         }
 
                         let lines = vec![Spans::from(info)];
@@ -206,7 +214,7 @@ impl View {
                                         Spans::from(Span::styled(format!("R: {}", v), style));
                                     spans.push(msg_log);
                                 }
-                                _ => return Err(DnetViewError::UnexpectedData),
+                                data => return Err(DnetViewError::UnexpectedData(data.to_string())),
                             }
                         }
                     }