Skip to content

Commit 90123b6

Browse files
rnijvelddavidv1992
authored andcommitted
[Monitor] Use eyre for error reporting
1 parent 957163c commit 90123b6

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nts-pool-monitor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ __internal-fuzz = []
1616
[dependencies]
1717
aead.workspace = true
1818
aes-siv.workspace = true
19+
eyre = { workspace = true }
1920
nts-pool-shared.workspace = true
2021
pool-nts.workspace = true
2122
rand.workspace = true

nts-pool-monitor/src/control.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use std::{
2828
sync::{Arc, RwLock},
2929
};
3030

31+
use eyre::Context;
3132
use nts_pool_shared::{ProbeControlCommand, ProbeResult};
3233
use serde::Serialize;
3334
use tokio::{
@@ -55,7 +56,7 @@ trait ProbeExecutor {
5556
fn run_probe(
5657
self: Arc<Self>,
5758
timesource: (IpVersion, String),
58-
) -> impl Future<Output = Result<Self::Output, std::io::Error>> + Send;
59+
) -> impl Future<Output = Result<Self::Output, eyre::Error>> + Send;
5960
}
6061

6162
impl ProbeExecutor for Probe {
@@ -78,7 +79,7 @@ impl ProbeExecutor for Probe {
7879
async fn run_probe(
7980
self: Arc<Self>,
8081
timesource: (IpVersion, String),
81-
) -> Result<Self::Output, std::io::Error> {
82+
) -> Result<Self::Output, eyre::Error> {
8283
self.probe(timesource.1, timesource.0).await
8384
}
8485
}
@@ -87,7 +88,7 @@ impl ProbeExecutor for Probe {
8788
trait ManagementRequestor {
8889
fn get_command(
8990
config: &ProbeControlConfig,
90-
) -> impl Future<Output = Result<ProbeControlCommand, std::io::Error>> + Send;
91+
) -> impl Future<Output = Result<ProbeControlCommand, eyre::Error>> + Send;
9192
}
9293

9394
async fn run_probing_inner<
@@ -104,7 +105,7 @@ async fn run_probing_inner<
104105
let command = match M::get_command(&config).await {
105106
Ok(command) => command,
106107
Err(e) => {
107-
error!("Could not fetch initial command: {}", e);
108+
error!("Could not fetch initial command: {:?}", e);
108109
std::process::exit(crate::exitcode::SOFTWARE);
109110
}
110111
};
@@ -311,16 +312,17 @@ async fn run_result_reporter<T: Send + Serialize + 'static>(
311312
struct ReqwestManagementRequestor;
312313

313314
impl ManagementRequestor for ReqwestManagementRequestor {
314-
async fn get_command(
315-
config: &ProbeControlConfig,
316-
) -> Result<ProbeControlCommand, std::io::Error> {
315+
async fn get_command(config: &ProbeControlConfig) -> Result<ProbeControlCommand, eyre::Error> {
317316
let result = reqwest::Client::new()
318317
.get(&config.management_interface)
319318
.bearer_auth(&config.authorization_key)
320319
.send()
321320
.await
322-
.map_err(std::io::Error::other)?;
323-
result.json().await.map_err(std::io::Error::other)
321+
.wrap_err("Failed to send probe request/get probe response")?;
322+
result
323+
.json()
324+
.await
325+
.wrap_err("Could not convert JSON response to probe command")
324326
}
325327
}
326328

@@ -371,7 +373,7 @@ mod tests {
371373
async fn run_probe(
372374
self: Arc<Self>,
373375
timesource: (IpVersion, String),
374-
) -> Result<Self::Output, std::io::Error> {
376+
) -> Result<Self::Output, eyre::Error> {
375377
Ok(timesource)
376378
}
377379
}
@@ -497,7 +499,7 @@ mod tests {
497499
impl ManagementRequestor for BasicCommandRequestor {
498500
async fn get_command(
499501
_config: &ProbeControlConfig,
500-
) -> Result<ProbeControlCommand, std::io::Error> {
502+
) -> Result<ProbeControlCommand, eyre::Error> {
501503
Ok(ProbeControlCommand {
502504
timesources: [
503505
(IpVersion::Ipv4, "A".to_string()),
@@ -571,7 +573,7 @@ mod tests {
571573
impl ManagementRequestor for SequencedCommandRequestor {
572574
async fn get_command(
573575
_config: &ProbeControlConfig,
574-
) -> Result<ProbeControlCommand, std::io::Error> {
576+
) -> Result<ProbeControlCommand, eyre::Error> {
575577
static INDEX: AtomicUsize = AtomicUsize::new(0);
576578
Ok(match INDEX.load(std::sync::atomic::Ordering::SeqCst) {
577579
0 => {

nts-pool-monitor/src/probe.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Probe {
6060
&self,
6161
uuid: impl AsRef<str>,
6262
ipprot: IpVersion,
63-
) -> Result<ProbeResult, std::io::Error> {
63+
) -> Result<ProbeResult, eyre::Error> {
6464
let (keyexchange, next) = self.probe_keyexchange(uuid, ipprot).await?;
6565

6666
let (ntp_with_ke_cookie, next) = match next {
@@ -84,7 +84,7 @@ impl Probe {
8484
&self,
8585
uuid: impl AsRef<str>,
8686
ipprot: IpVersion,
87-
) -> Result<(KeyExchangeProbeResult, Option<NtpInputs>), std::io::Error> {
87+
) -> Result<(KeyExchangeProbeResult, Option<NtpInputs>), eyre::Error> {
8888
let addr = resolve_as_version((self.poolke.as_str(), 4460), ipprot).await?;
8989
let io = TcpStream::connect(addr).await?;
9090

@@ -100,14 +100,7 @@ impl Probe {
100100
.await
101101
{
102102
Ok(Ok(result)) => result,
103-
Ok(Err(NtsError::IO(e))) => return Err(e),
104-
Ok(Err(NtsError::Tls(e))) => return Err(std::io::Error::other(e)),
105-
Ok(Err(NtsError::Dns(e))) => return Err(std::io::Error::other(e)),
106-
Ok(Err(NtsError::UnrecognizedCriticalRecord))
107-
| Ok(Err(NtsError::UnknownWarning(_))) => {
108-
return Err(std::io::ErrorKind::InvalidData.into());
109-
}
110-
Ok(Err(_)) => {
103+
Ok(Err(NtsError::Invalid | NtsError::Error(_))) => {
111104
let end_time = Instant::now();
112105
return Ok((
113106
KeyExchangeProbeResult {
@@ -119,6 +112,7 @@ impl Probe {
119112
None,
120113
));
121114
}
115+
Ok(Err(e)) => return Err(e.into()),
122116
Err(_) => {
123117
let end_time = Instant::now();
124118
return Ok((

0 commit comments

Comments
 (0)