commands: more consistent error propagation

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a69647a0eb8de028e4251465fbb94f0a14cef
This commit is contained in:
raf 2025-08-20 11:14:51 +03:00
commit c9a73b462d
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
4 changed files with 66 additions and 71 deletions

View file

@ -24,26 +24,33 @@ impl DecodeCommand for SqliteClipboardDb {
s
} else {
let mut buf = String::new();
if let Err(e) = in_.read_to_string(&mut buf) {
log::error!("Failed to read stdin for decode: {e}");
}
in_
.read_to_string(&mut buf)
.map_err(|e| StashError::DecodeRead(e.to_string()))?;
buf
};
// If input is empty or whitespace, treat as error and trigger fallback
if input_str.trim().is_empty() {
log::info!("No input provided to decode; relaying clipboard to stdout");
log::debug!("No input provided to decode; relaying clipboard to stdout");
if let Ok((mut reader, _mime)) =
get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Any)
{
let mut buf = Vec::new();
if let Err(err) = reader.read_to_end(&mut buf) {
log::error!("Failed to read clipboard for relay: {err}");
} else {
let _ = out.write_all(&buf);
}
reader.read_to_end(&mut buf).map_err(|e| {
StashError::DecodeRead(format!(
"Failed to read clipboard for relay: {e}"
))
})?;
out.write_all(&buf).map_err(|e| {
StashError::DecodeWrite(format!(
"Failed to write clipboard relay: {e}"
))
})?;
} else {
log::error!("Failed to get clipboard contents for relay");
return Err(StashError::DecodeGet(
"Failed to get clipboard contents for relay".to_string(),
));
}
return Ok(());
}
@ -54,25 +61,28 @@ impl DecodeCommand for SqliteClipboardDb {
&mut out,
Some(input_str.clone()),
) {
Ok(()) => {
log::info!("Entry decoded");
},
Ok(()) => Ok(()),
Err(e) => {
log::error!("Failed to decode entry: {e}");
// On decode failure, relay clipboard as fallback
if let Ok((mut reader, _mime)) =
get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Any)
{
let mut buf = Vec::new();
if let Err(err) = reader.read_to_end(&mut buf) {
log::error!("Failed to read clipboard for relay: {err}");
} else {
let _ = out.write_all(&buf);
}
reader.read_to_end(&mut buf).map_err(|err| {
StashError::DecodeRead(format!(
"Failed to read clipboard for relay: {err}"
))
})?;
out.write_all(&buf).map_err(|err| {
StashError::DecodeWrite(format!(
"Failed to write clipboard relay: {err}"
))
})?;
Ok(())
} else {
log::error!("Failed to get clipboard contents for relay");
Err(e)
}
},
}
Ok(())
}
}