Skip to content

Commit c7563ff

Browse files
feat: introduce explicit error handling
Ref n0-computer/iroh#2741
1 parent 06fee28 commit c7563ff

File tree

6 files changed

+128
-136
lines changed

6 files changed

+128
-136
lines changed

Cargo.lock

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

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ unexpected_cfgs = { level = "warn", check-cfg = ["cfg(iroh_docsrs)"] }
2727
unused-async = "warn"
2828

2929
[dependencies]
30-
anyhow = { version = "1" }
3130
erased_set = "0.8"
3231
http-body-util = "0.1.0"
3332
hyper = { version = "1", features = ["server", "http1"] }
@@ -37,6 +36,7 @@ prometheus-client = { version = "0.22", optional = true }
3736
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
3837
serde = { version = "1", features = ["derive"] }
3938
struct_iterable = "0.1"
39+
thiserror = "2.0.6"
4040
time = { version = "0.3.21", features = ["serde-well-known"] }
4141
tokio = { version = "1", features = ["rt", "net", "fs"]}
4242
tracing = "0.1"

src/core.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ impl Core {
177177
/// Encodes the current metrics registry to a string in
178178
/// the prometheus text exposition format.
179179
#[cfg(feature = "metrics")]
180-
pub fn encode(&self) -> Result<String, std::fmt::Error> {
180+
pub fn encode(&self) -> String {
181181
let mut buf = String::new();
182-
encode(&mut buf, &self.registry)?;
183-
Ok(buf)
182+
encode(&mut buf, &self.registry).expect("writing to string always works");
183+
buf
184184
}
185185
}
186186

src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ use std::collections::HashMap;
1515
/// Reexport to make matching versions easier.
1616
pub use struct_iterable;
1717

18+
/// Potential errors from this library.
19+
#[derive(Debug, thiserror::Error)]
20+
pub enum Error {
21+
/// Indicates that the metrics have not been enabled.
22+
#[error("Metrics not enabled")]
23+
NoMetrics,
24+
/// Any IO related error.
25+
#[error("IO: {0}")]
26+
Io(#[from] std::io::Error),
27+
}
28+
1829
/// Increment the given counter by 1.
1930
#[macro_export]
2031
macro_rules! inc {
@@ -40,7 +51,7 @@ macro_rules! set {
4051
}
4152

4253
/// Parse Prometheus metrics from a string.
43-
pub fn parse_prometheus_metrics(data: &str) -> anyhow::Result<HashMap<String, f64>> {
54+
pub fn parse_prometheus_metrics(data: &str) -> HashMap<String, f64> {
4455
let mut metrics = HashMap::new();
4556
for line in data.lines() {
4657
if line.starts_with('#') {
@@ -57,7 +68,7 @@ pub fn parse_prometheus_metrics(data: &str) -> anyhow::Result<HashMap<String, f6
5768
}
5869
metrics.insert(metric.to_string(), value.unwrap());
5970
}
60-
Ok(metrics)
71+
metrics
6172
}
6273

6374
/// Configuration for pushing metrics to a remote endpoint.

src/metrics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use std::net::SocketAddr;
5454

5555
/// Start a server to serve the OpenMetrics endpoint.
5656
#[cfg(feature = "metrics")]
57-
pub async fn start_metrics_server(addr: SocketAddr) -> anyhow::Result<()> {
57+
pub async fn start_metrics_server(addr: SocketAddr) -> std::io::Result<()> {
5858
crate::service::run(addr).await
5959
}
6060

@@ -63,7 +63,7 @@ pub async fn start_metrics_server(addr: SocketAddr) -> anyhow::Result<()> {
6363
pub async fn start_metrics_dumper(
6464
path: std::path::PathBuf,
6565
interval: std::time::Duration,
66-
) -> anyhow::Result<()> {
66+
) -> Result<(), crate::Error> {
6767
crate::service::dumper(&path, interval).await
6868
}
6969

0 commit comments

Comments
 (0)