Improved error handling

This commit is contained in:
hodasemi 2022-07-25 17:42:27 +02:00
parent 4ffffdeec1
commit a5a755ad41
3 changed files with 22 additions and 18 deletions

24
.vscode/launch.json vendored
View file

@ -7,15 +7,15 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'service'",
"name": "Debug executable 'macroboard_service'",
"cargo": {
"args": [
"build",
"--bin=service",
"--bin=macroboard_service",
"--package=macroboard"
],
"filter": {
"name": "service",
"name": "macroboard_service",
"kind": "bin"
}
},
@ -25,16 +25,16 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'service'",
"name": "Debug unit tests in executable 'macroboard_service'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=service",
"--bin=macroboard_service",
"--package=macroboard"
],
"filter": {
"name": "service",
"name": "macroboard_service",
"kind": "bin"
}
},
@ -44,15 +44,15 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'gui'",
"name": "Debug executable 'macroboard_gui'",
"cargo": {
"args": [
"build",
"--bin=gui",
"--bin=macroboard_gui",
"--package=macroboard"
],
"filter": {
"name": "gui",
"name": "macroboard_gui",
"kind": "bin"
}
},
@ -62,16 +62,16 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'gui'",
"name": "Debug unit tests in executable 'macroboard_gui'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=gui",
"--bin=macroboard_gui",
"--package=macroboard"
],
"filter": {
"name": "gui",
"name": "macroboard_gui",
"kind": "bin"
}
},

View file

@ -61,6 +61,7 @@ fn main() -> Result<()> {
message_builder = MessageBuilder::default();
}
}
SerialReadResult::UtfConversion(err) => println!("{:?}", err),
SerialReadResult::Timeout => (),
}
}
@ -93,7 +94,9 @@ fn execute_button_press(
if !cmd.is_empty() {
println!("cmd: {}", cmd);
Command::new(cmd).spawn()?;
if let Err(err) = Command::new(cmd).spawn() {
println!("failed with: {:?}", err);
}
}
}

View file

@ -1,12 +1,13 @@
use anyhow::{anyhow, Result};
use std::io;
use std::{io, string::FromUtf8Error};
pub use serialport::{DataBits, FlowControl, Parity, SerialPort, StopBits};
pub use std::time::Duration;
pub enum SerialReadResult {
Message(String),
UtfConversion(FromUtf8Error),
Timeout,
}
@ -50,10 +51,10 @@ impl Port {
let mut buf: Vec<u8> = (0..255).collect();
match self.serial_port.read(&mut buf[..]) {
Ok(t) => Ok(SerialReadResult::Message(
String::from_utf8(buf[0..t].to_vec())
.map_err(|err| anyhow!("failed converting utf8 buffer ({})", err))?,
)),
Ok(t) => match String::from_utf8(buf[0..t].to_vec()) {
Ok(s) => Ok(SerialReadResult::Message(s)),
Err(err) => Ok(SerialReadResult::UtfConversion(err)),
},
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(SerialReadResult::Timeout),
Err(err) => Err(anyhow!("failed reading serial port ({})", err)),
}