From 4edebb22ab8d5458d8eed599f8eb5db09f47dbee Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 4 May 2022 08:49:16 +0200 Subject: [PATCH] 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 --- bin/dnetview/src/error.rs | 2 +- bin/dnetview/src/view.rs | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/bin/dnetview/src/error.rs b/bin/dnetview/src/error.rs index e11fb32e1..95adbebad 100644 --- a/bin/dnetview/src/error.rs +++ b/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")] diff --git a/bin/dnetview/src/view.rs b/bin/dnetview/src/view.rs index 231ecb2ff..ff51ecf44 100644 --- a/bin/dnetview/src/view.rs +++ b/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())), } } }