Skip to content

Commit 9befd31

Browse files
committed
anyhow -> thiserror
1 parent 6e59b49 commit 9befd31

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

Cargo.lock

+23-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ros-monitor-lib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ version = "0.1.1"
44
edition = "2021"
55

66
[dependencies]
7-
anyhow = "1.0.75"
87
async-stream = "0.3.6"
98
bitcode = "0.6.3"
109
futures = "0.3.30"
1110
log = "0.4.21"
1211
serde = { version = "1.0.188", features = ["derive"] }
1312
serde_json = "1.0.107"
13+
thiserror = "2.0.9"
1414
tokio = { version = "1.32.0", features = ["io-util", "process", "rt", "sync", "time"] }
1515

1616
[dev-dependencies]

ros-monitor-lib/src/lib.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::ffi::OsString;
22
use std::sync::{Arc, Mutex};
33

4-
use anyhow::Context;
4+
use thiserror::Error;
5+
use tokio::sync::broadcast::error::RecvError;
56

67
pub mod types;
78
pub mod state;
@@ -35,7 +36,7 @@ impl RosMonitor {
3536
let channel_arc_ = channel_arc.clone();
3637

3738
let task = AbortJoinHandle(tokio::spawn(async move {
38-
let error: Result<(), anyhow::Error> = async {
39+
let error: Result<(), RosMonitorError> = async {
3940
let channel_ = {
4041
let channel = channel_arc_.lock().unwrap();
4142
channel.as_ref().unwrap().clone()
@@ -53,7 +54,7 @@ impl RosMonitor {
5354
.stderr(Stdio::piped())
5455
.spawn()?;
5556

56-
let stdout = child.stdout.take().context("unable to capture stdout")?;
57+
let stdout = child.stdout.take().ok_or(RosMonitorError::PipeError)?;
5758
let mut reader = tokio::io::BufReader::new(stdout);
5859

5960
loop {
@@ -86,11 +87,14 @@ impl RosMonitor {
8687

8788
let elapsed = started_at.elapsed();
8889
if elapsed.as_millis() < 2000 {
89-
let mut stderr = child.stderr.take().context("unable to capture stderr")?;
90+
let mut stderr = child.stderr.take().ok_or(RosMonitorError::PipeError)?;
9091
let mut result = String::new();
9192
stderr.read_to_string(&mut result).await?;
9293
let status = child.try_wait()?;
93-
Err(anyhow::anyhow!("process exited: {}\n{}", status.unwrap_or_default(), result))?;
94+
Err(RosMonitorError::ProcessExited {
95+
status: status.unwrap_or_default(),
96+
stderr: result,
97+
})?;
9498
}
9599
}
96100
}.await;
@@ -113,9 +117,9 @@ impl RosMonitor {
113117
}
114118
}
115119

116-
pub fn subscribe(&self) -> anyhow::Result<impl futures::TryStream<Item = anyhow::Result<types::DiscoveryEvent>>> {
120+
pub fn subscribe(&self) -> Result<impl futures::TryStream<Item = Result<types::DiscoveryEvent, RecvError>>, RecvError> {
117121
if self.task.0.is_finished() {
118-
return Err(anyhow::anyhow!("monitor is not running"));
122+
return Err(RecvError::Closed);
119123
}
120124

121125
let (initial, receiver) = {
@@ -128,7 +132,7 @@ impl RosMonitor {
128132
};
129133

130134
Ok(async_stream::try_stream! {
131-
let mut receiver = receiver.context("channel closed")?;
135+
let mut receiver = receiver.ok_or(RecvError::Closed)?;
132136
for event in initial {
133137
yield event;
134138
}
@@ -139,3 +143,16 @@ impl RosMonitor {
139143
})
140144
}
141145
}
146+
147+
#[derive(Debug, Error)]
148+
enum RosMonitorError {
149+
#[error("unable to spawn process: {0}")]
150+
SpawnError(#[from] tokio::io::Error),
151+
#[error("unable to read stdout/stderr")]
152+
PipeError,
153+
#[error("process exited: {status}\n{stderr}")]
154+
ProcessExited {
155+
status: std::process::ExitStatus,
156+
stderr: String,
157+
},
158+
}

0 commit comments

Comments
 (0)