From 22f743a73062819c76a5cab3503aee52f884ce79 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 19 Oct 2019 13:35:03 -0400 Subject: display connection errors in the watch ui --- src/client.rs | 21 +++++++++++++++++++-- src/cmd/watch.rs | 16 +++++++++++----- src/error.rs | 2 +- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/client.rs b/src/client.rs index 80a7455..fc9c07a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -96,6 +96,8 @@ pub struct Client< on_login: Vec, to_send: std::collections::VecDeque, + + last_error: Option, } impl @@ -164,6 +166,8 @@ impl on_login: on_login.to_vec(), to_send: std::collections::VecDeque::new(), + + last_error: None, } } @@ -176,6 +180,10 @@ impl self.wsock = WriteSocket::NotConnected; } + pub fn last_error(&self) -> Option<&str> { + self.last_error.as_ref().map(std::string::String::as_str) + } + fn set_reconnect_timer(&mut self) { let delay = rand::thread_rng().gen_range( self.reconnect_backoff_amount / 2, @@ -269,6 +277,7 @@ impl for msg in &self.on_login { self.to_send.push_back(msg.clone()); } + self.last_error = None; Ok(( crate::component_future::Async::Ready(Some( Event::Connect, @@ -421,8 +430,10 @@ impl Err(e) => { log::warn!("error while connecting, reconnecting: {}", e); self.reconnect(); - // not sending a disconnect event here because we never - // actually connected, so it'd just be spammy + self.last_error = Some(format!("{}", e)); + return Ok(crate::component_future::Async::Ready(Some( + Event::Disconnect, + ))); } }, WriteSocket::Connected(..) | WriteSocket::Writing(..) => { @@ -433,6 +444,8 @@ impl "haven't seen server in a while, reconnecting", ); self.reconnect(); + self.last_error = + Some("haven't seen server in a while".to_string()); return Ok(crate::component_future::Async::Ready(Some( Event::Disconnect, ))); @@ -480,6 +493,7 @@ impl e ); self.reconnect(); + self.last_error = Some(format!("{}", e)); Ok(crate::component_future::Async::Ready(Some( Event::Disconnect, ))) @@ -492,6 +506,7 @@ impl Err(e) => { log::warn!("error reading message, reconnecting: {}", e); self.reconnect(); + self.last_error = Some(format!("{}", e)); Ok(crate::component_future::Async::Ready(Some( Event::Disconnect, ))) @@ -519,6 +534,7 @@ impl e ); self.reconnect(); + self.last_error = Some(format!("{}", e)); Ok(crate::component_future::Async::Ready(Some( Event::Disconnect, ))) @@ -564,6 +580,7 @@ impl Err(e) => { log::warn!("error writing message, reconnecting: {}", e); self.reconnect(); + self.last_error = Some(format!("{}", e)); Ok(crate::component_future::Async::Ready(Some( Event::Disconnect, ))) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 284fc0e..31cd505 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -443,11 +443,17 @@ impl crossterm::terminal() .clear(crossterm::ClearType::All) .context(crate::error::WriteTerminalCrossterm)?; - let data = b"loading...\r\nq: quit --> "; - let stdout = std::io::stdout(); - let mut stdout = stdout.lock(); - stdout.write(data).context(crate::error::WriteTerminal)?; - stdout.flush().context(crate::error::FlushTerminal)?; + + println!("loading...\r"); + if let Some(err) = self.list_client.last_error() { + println!("error: {}\r", err); + } + print!("q: quit --> "); + + std::io::stdout() + .flush() + .context(crate::error::FlushTerminal)?; + Ok(()) } diff --git a/src/error.rs b/src/error.rs index 7a3f226..eab1c78 100644 --- a/src/error.rs +++ b/src/error.rs @@ -50,7 +50,7 @@ pub enum Error { source: tokio::io::Error, }, - #[snafu(display("eof"))] + #[snafu(display("received EOF from server"))] EOF, #[snafu(display("failed to retrieve access token: {:?}", msg))] -- cgit v1.2.3-54-g00ecf