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

View file

@ -61,6 +61,7 @@ fn main() -> Result<()> {
message_builder = MessageBuilder::default(); message_builder = MessageBuilder::default();
} }
} }
SerialReadResult::UtfConversion(err) => println!("{:?}", err),
SerialReadResult::Timeout => (), SerialReadResult::Timeout => (),
} }
} }
@ -93,7 +94,9 @@ fn execute_button_press(
if !cmd.is_empty() { if !cmd.is_empty() {
println!("cmd: {}", cmd); 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 anyhow::{anyhow, Result};
use std::io; use std::{io, string::FromUtf8Error};
pub use serialport::{DataBits, FlowControl, Parity, SerialPort, StopBits}; pub use serialport::{DataBits, FlowControl, Parity, SerialPort, StopBits};
pub use std::time::Duration; pub use std::time::Duration;
pub enum SerialReadResult { pub enum SerialReadResult {
Message(String), Message(String),
UtfConversion(FromUtf8Error),
Timeout, Timeout,
} }
@ -50,10 +51,10 @@ impl Port {
let mut buf: Vec<u8> = (0..255).collect(); let mut buf: Vec<u8> = (0..255).collect();
match self.serial_port.read(&mut buf[..]) { match self.serial_port.read(&mut buf[..]) {
Ok(t) => Ok(SerialReadResult::Message( Ok(t) => match String::from_utf8(buf[0..t].to_vec()) {
String::from_utf8(buf[0..t].to_vec()) Ok(s) => Ok(SerialReadResult::Message(s)),
.map_err(|err| anyhow!("failed converting utf8 buffer ({})", err))?, Err(err) => Ok(SerialReadResult::UtfConversion(err)),
)), },
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(SerialReadResult::Timeout), Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(SerialReadResult::Timeout),
Err(err) => Err(anyhow!("failed reading serial port ({})", err)), Err(err) => Err(anyhow!("failed reading serial port ({})", err)),
} }