Skip to content

Commit fad99ab

Browse files
refactor(iroh)!: simplify discovery errors (#3340)
## Description A couple of variants where only used internally, and the the `NodeId` variant had the same semantics as `NoResults`, so they can be merged. ## Breaking Changes - Some variants of the `iroh::discovery::DiscoveryError` have been changed ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme) --------- Co-authored-by: Friedel Ziegelmayer <me@dignifiedquire.com>
1 parent 444c76b commit fad99ab

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

iroh/src/discovery.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@
107107
108108
use std::sync::Arc;
109109

110-
use iroh_base::{NodeAddr, NodeId, PublicKey};
111-
use iroh_relay::node_info::EncodingError;
110+
use iroh_base::{NodeAddr, NodeId};
112111
use n0_future::{
113112
boxed::BoxStream,
114113
stream::StreamExt,
@@ -144,20 +143,8 @@ pub mod static_provider;
144143
pub enum DiscoveryError {
145144
#[snafu(display("No discovery service configured"))]
146145
NoServiceConfigured {},
147-
#[snafu(display("Cannot resolve node id"))]
148-
NodeId { node_id: PublicKey },
149-
#[snafu(display("Discovery produced no results"))]
150-
NoResults { node_id: PublicKey },
151-
#[snafu(display("Error encoding the signed packet"))]
152-
SignedPacket {
153-
#[snafu(source(from(EncodingError, Box::new)))]
154-
source: Box<EncodingError>,
155-
},
156-
#[snafu(display("Error parsing the signed packet"))]
157-
ParsePacket {
158-
#[snafu(source(from(ParseError, Box::new)))]
159-
source: Box<ParseError>,
160-
},
146+
#[snafu(display("Discovery produced no results for {}", node_id.fmt_short()))]
147+
NoResults { node_id: NodeId },
161148
#[snafu(display("Service '{provenance}' error"))]
162149
User {
163150
provenance: &'static str,
@@ -491,7 +478,7 @@ impl DiscoveryTask {
491478
let discovery = ep.discovery().ok_or(NoServiceConfiguredSnafu.build())?;
492479
let stream = discovery
493480
.resolve(ep.clone(), node_id)
494-
.ok_or(NodeIdSnafu { node_id }.build())?;
481+
.ok_or(NoResultsSnafu { node_id }.build())?;
495482
Ok(stream)
496483
}
497484

iroh/src/discovery/pkarr.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
use std::sync::Arc;
4848

4949
use iroh_base::{NodeId, RelayUrl, SecretKey};
50-
use iroh_relay::node_info::NodeInfo;
50+
use iroh_relay::node_info::{EncodingError, NodeInfo};
5151
use n0_future::{
5252
boxed::BoxStream,
5353
task::{self, AbortOnDropHandle},
@@ -63,7 +63,7 @@ use url::Url;
6363

6464
use super::DiscoveryError;
6565
use crate::{
66-
discovery::{Discovery, DiscoveryItem, NodeData, ParsePacketSnafu, SignedPacketSnafu},
66+
discovery::{Discovery, DiscoveryItem, NodeData},
6767
endpoint::force_staging_infra,
6868
watcher::{self, Disconnected, Watchable, Watcher as _},
6969
Endpoint,
@@ -88,6 +88,8 @@ pub enum PkarrError {
8888
HttpRequest { status: reqwest::StatusCode },
8989
#[snafu(display("Http payload error"))]
9090
HttpPayload { source: reqwest::Error },
91+
#[snafu(display("EncodingError"))]
92+
Encoding { source: EncodingError },
9193
}
9294

9395
impl From<PkarrError> for DiscoveryError {
@@ -291,15 +293,15 @@ impl PublisherService {
291293
}
292294
}
293295

294-
async fn publish_current(&self, info: NodeInfo) -> Result<(), DiscoveryError> {
296+
async fn publish_current(&self, info: NodeInfo) -> Result<(), PkarrError> {
295297
debug!(
296298
data = ?info.data,
297299
pkarr_relay = %self.pkarr_client.pkarr_relay_url,
298300
"Publish node info to pkarr"
299301
);
300302
let signed_packet = info
301303
.to_pkarr_signed_packet(&self.secret_key, self.ttl)
302-
.context(SignedPacketSnafu)?;
304+
.context(EncodingSnafu)?;
303305
self.pkarr_client.publish(&signed_packet).await?;
304306
Ok(())
305307
}
@@ -360,8 +362,8 @@ impl Discovery for PkarrResolver {
360362
let pkarr_client = self.pkarr_client.clone();
361363
let fut = async move {
362364
let signed_packet = pkarr_client.resolve(node_id).await?;
363-
let info =
364-
NodeInfo::from_pkarr_signed_packet(&signed_packet).context(ParsePacketSnafu)?;
365+
let info = NodeInfo::from_pkarr_signed_packet(&signed_packet)
366+
.map_err(|err| DiscoveryError::from_err("pkarr", err))?;
365367
let item = DiscoveryItem::new(info, "pkarr", None);
366368
Ok(item)
367369
};
@@ -426,7 +428,7 @@ impl PkarrRelayClient {
426428
}
427429

428430
/// Publishes a [`SignedPacket`].
429-
pub async fn publish(&self, signed_packet: &SignedPacket) -> Result<(), DiscoveryError> {
431+
pub async fn publish(&self, signed_packet: &SignedPacket) -> Result<(), PkarrError> {
430432
let mut url = self.pkarr_relay_url.clone();
431433
url.path_segments_mut()
432434
.map_err(|_| {
@@ -449,8 +451,7 @@ impl PkarrRelayClient {
449451
return Err(HttpRequestSnafu {
450452
status: response.status(),
451453
}
452-
.build()
453-
.into());
454+
.build());
454455
}
455456

456457
Ok(())

0 commit comments

Comments
 (0)