aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/sock.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-04-12 00:08:03 -0400
committerJesse Luehrs <doy@tozt.net>2020-04-12 00:08:03 -0400
commit236f06736e45c2a70f43589c9d447a0a3ef240b5 (patch)
treec390d4cbfec5223ac1aefe3947f8e1bb885757d2 /src/bin/rbw/sock.rs
parent91d1d1890bdc3ee75b69e00d5368c5b29a4f461c (diff)
downloadrbw-236f06736e45c2a70f43589c9d447a0a3ef240b5.tar.gz
rbw-236f06736e45c2a70f43589c9d447a0a3ef240b5.zip
improve error handling and reporting
Diffstat (limited to 'src/bin/rbw/sock.rs')
-rw-r--r--src/bin/rbw/sock.rs31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/bin/rbw/sock.rs b/src/bin/rbw/sock.rs
index 05597e6..4534e5e 100644
--- a/src/bin/rbw/sock.rs
+++ b/src/bin/rbw/sock.rs
@@ -1,29 +1,38 @@
+use anyhow::Context as _;
use std::io::{BufRead as _, Write as _};
pub struct Sock(std::os::unix::net::UnixStream);
impl Sock {
- pub fn connect() -> Self {
- Self(
+ pub fn connect() -> anyhow::Result<Self> {
+ Ok(Self(
std::os::unix::net::UnixStream::connect(
rbw::dirs::runtime_dir().join("socket"),
)
- .unwrap(),
- )
+ .context("failed to connect to rbw-agent")?,
+ ))
}
- pub fn send(&mut self, msg: &rbw::agent::Request) {
+ pub fn send(&mut self, msg: &rbw::agent::Request) -> anyhow::Result<()> {
let Self(sock) = self;
- sock.write_all(serde_json::to_string(msg).unwrap().as_bytes())
- .unwrap();
- sock.write_all(b"\n").unwrap();
+ sock.write_all(
+ serde_json::to_string(msg)
+ .context("failed to serialize message to agent")?
+ .as_bytes(),
+ )
+ .context("failed to send message to agent")?;
+ sock.write_all(b"\n")
+ .context("failed to send message to agent")?;
+ Ok(())
}
- pub fn recv(&mut self) -> rbw::agent::Response {
+ pub fn recv(&mut self) -> anyhow::Result<rbw::agent::Response> {
let Self(sock) = self;
let mut buf = std::io::BufReader::new(sock);
let mut line = String::new();
- buf.read_line(&mut line).unwrap();
- serde_json::from_str(&line).unwrap()
+ buf.read_line(&mut line)
+ .context("failed to read message from agent")?;
+ Ok(serde_json::from_str(&line)
+ .context("failed to parse message from agent")?)
}
}