Skip to content

Commit

Permalink
Add error context to functions executed during button press
Browse files Browse the repository at this point in the history
Errors during executing button press action are hard to debug as its unclear what exactly is causing them.
This change adds a context to all executed functions.
  • Loading branch information
Jan Rüth committed May 8, 2024
1 parent cff1bbb commit 0154bb8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion es2button/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "es2button"
version = "0.3.0"
version = "0.3.1"
authors = ["Jan Rüth <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
16 changes: 9 additions & 7 deletions es2button/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ impl<'a> Listener<'a> {
.get_bool_value_for_key(es2command::ESKEY_CARD_SCANNING)
}

fn handle_button_press(&self, button: u8) -> Result<(), ESError> {
fn handle_button_press(&self, button: u8) -> Result<(), anyhow::Error> {
debug!("Button pressed: {}", button);

trace!("Checking scanner document state");
let doc_present = self.is_document_loaded()?;
let card_scanning = self.is_card_scanning()?;
let doc_present = self
.is_document_loaded()
.context("checking document status")?;
let card_scanning = self.is_card_scanning().context("checking card scanning")?;

trace!("Disconnecting from scanner");
self.scanner.close()?;
self.scanner.close().context("closing scanner")?;

trace!("Spawning process");
let output = Command::new(&self.program)
Expand All @@ -107,7 +109,7 @@ impl<'a> Listener<'a> {
.env("ES2_DEV_VID", format!("{:03}", self.device.vid))
.env("ES2_DEV_PID", format!("{:03}", self.device.pid))
.output()
.expect("failed executing program");
.expect("program execution failed"); // do not error here as we can't open the scanner otherwise, user needs to fix their script

info!("Script status: {}", output.status);
info!("Script stdout:");
Expand All @@ -117,7 +119,7 @@ impl<'a> Listener<'a> {
info!("Resuming from script");

trace!("Reopening connection to scanner");
self.scanner.open()
self.scanner.open().context("opening scanner")
}
}

Expand All @@ -135,7 +137,7 @@ impl<'a> es2command::ScanDelegate for Listener<'a> {

fn did_press_button(&self, button: u8) {
if let Err(err) = self.handle_button_press(button) {
error!("failed handling button press: {}", err);
error!("failed handling button press: {:#}", err);
}
}

Expand Down

0 comments on commit 0154bb8

Please sign in to comment.