commit 508c5c2ee266ae65535e87e021eecd16424417c3
parent db2e55d8508d663d94f9e5f9860d8756d6f9a157
Author: Sylvia Ivory <git@sivory.net>
Date: Tue, 10 Feb 2026 15:23:17 -0800
Improve error messages
Diffstat:
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/tools/src/tcp.rs b/tools/src/tcp.rs
@@ -12,7 +12,7 @@ use tokio::{
},
sync::RwLock,
};
-use tracing::{Instrument, Level, debug, error, field, info, instrument, span};
+use tracing::{Instrument, Level, debug, error, field, info, instrument, span, warn};
pub type ConnectionId = u16;
@@ -57,8 +57,8 @@ impl From<&Message> for u8 {
#[derive(thiserror::Error, Debug)]
pub enum Error {
- #[error("Invalid message kind")]
- InvalidKind,
+ #[error("invalid message kind: {0}")]
+ InvalidKind(u8),
#[error("io: {0}")]
Io(#[from] std::io::Error),
}
@@ -108,12 +108,12 @@ pub async fn parse_message<R: AsyncRead + Unpin>(
r.read_exact(&mut data).await?;
Ok(Message::Print { length, data })
}
- _ => {
+ k => {
if log_err {
- tracing::error!(kind = kind, "invalid message kind");
+ error!(kind = kind, "invalid message kind");
}
- Err(Error::InvalidKind)
+ Err(Error::InvalidKind(k))
}
}
}
@@ -136,7 +136,7 @@ pub async fn send_message<W: AsyncWrite + Unpin>(w: &mut W, msg: &Message) -> Re
Message::Disconnect(id) => {
w.write_u16_le(*id).await?;
}
- _ => return Err(Error::InvalidKind),
+ m => return Err(Error::InvalidKind(m.into())),
}
w.flush().await?;
@@ -246,7 +246,18 @@ async fn port_handler<R: AsyncRead + Unpin>(
map: Arc<RwLock<HashMap<ConnectionId, OwnedWriteHalf>>>,
) -> Result<(), Error> {
loop {
- let msg = parse_message(&mut r, true).await?;
+ let msg = match parse_message(&mut r, true).await {
+ Err(Error::InvalidKind(k)) => {
+ warn!(kind = k, "invalid message kind");
+ continue;
+ }
+ Err(Error::Io(ref e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
+ warn!("serial port disconnected, exiting");
+ return Ok(());
+ }
+ Err(e) => return Err(e),
+ Ok(m) => m,
+ };
match msg {
Message::Write {