Skip to content

Commit

Permalink
feat: p2p api service calls with a p2p-specific error, better feedback (
Browse files Browse the repository at this point in the history
#402)

Co-authored-by: Kasey <[email protected]>
  • Loading branch information
b5 and ramfox authored Oct 24, 2022
1 parent aba1b17 commit b565231
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions iroh-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async-stream = "0.3.3"
mockall = { version = "0.11.2", optional = true }
serde = { version = "1.0", features = ["derive"] }
relative-path = "1.7.2"
thiserror = "1.0"

[dev-dependencies]
tempfile = "3.3.0"
27 changes: 27 additions & 0 deletions iroh-api/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::io;
use thiserror::Error as ThisError;
// use std::error::Error;
use anyhow::{anyhow, Error};

/// LockError is the set of known program lock errors
#[derive(ThisError, Debug)]
pub enum ApiError<'a> {
#[error("Can't connect to {service}. Is the service running?")]
ConnectionRefused { service: &'a str },
/// catchall error type
#[error("{source}")]
Uncategorized {
#[from]
source: anyhow::Error,
},
}

pub fn map_service_error(service: &'static str, e: Error) -> Error {
let io_error = e.root_cause().downcast_ref::<io::Error>();
if let Some(io_error) = io_error {
if io_error.kind() == io::ErrorKind::ConnectionRefused {
return anyhow!(ApiError::ConnectionRefused { service });
}
}
e
}
2 changes: 2 additions & 0 deletions iroh-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod api;
mod api_ext;
mod config;
mod error;
mod p2p;

#[cfg(feature = "testing")]
pub use crate::api::MockApi;
pub use crate::api::{Api, Iroh, OutType};
pub use crate::api_ext::ApiExt;
pub use crate::error::ApiError;
#[cfg(feature = "testing")]
pub use crate::p2p::MockP2p;
pub use crate::p2p::P2p as P2pApi;
Expand Down
9 changes: 8 additions & 1 deletion iroh-api/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::map_service_error;
use anyhow::Result;
use async_trait::async_trait;
use iroh_rpc_client::{Lookup, P2pClient};
Expand Down Expand Up @@ -32,7 +33,11 @@ pub trait P2p: Sync {
#[async_trait]
impl P2p for ClientP2p {
async fn lookup_local(&self) -> Result<Lookup> {
let (_, listen_addrs) = self.client.get_listening_addrs().await?;
let (_, listen_addrs) = self
.client
.get_listening_addrs()
.await
.map_err(|e| map_service_error("p2p", e))?;
Ok(Lookup {
peer_id: self.client.local_peer_id().await?,
listen_addrs,
Expand All @@ -51,6 +56,7 @@ impl P2p for ClientP2p {
self.client.lookup(peer_id, Some(addr.clone())).await
}
}
.map_err(|e| map_service_error("p2p", e))
}

async fn connect(&self, addr: &PeerIdOrAddr) -> Result<()> {
Expand All @@ -61,6 +67,7 @@ impl P2p for ClientP2p {
self.client.connect(peer_id, vec![addr.clone()]).await
}
}
.map_err(|e| map_service_error("p2p", e))
}
}

Expand Down
20 changes: 19 additions & 1 deletion iroh/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use crossterm::style::Stylize;
use iroh_api::ApiError;
use std::io;

#[tokio::main(flavor = "multi_thread")]
Expand Down Expand Up @@ -30,10 +32,26 @@ fn transform_error(r: Result<()>) -> Result<()> {
if let Some(io_error) = io_error {
if io_error.kind() == io::ErrorKind::ConnectionRefused {
return Err(anyhow!(
"Connection refused. Are `iroh-p2p` and `iroh-store` running?"
"Connection refused. Are services running?\n{}",
"hint: see 'iroh start' for more on starting services".yellow(),
));
}
}
let api_error = e.root_cause().downcast_ref::<ApiError>();
if let Some(api_error) = api_error {
match api_error {
ApiError::ConnectionRefused { service } => {
return Err(anyhow!(
"Connection refused. This command requires a running {} service.\n{}",
service,
format!("hint: try 'iroh start {}'", service).yellow(),
));
}
_ => {
return Err(e);
}
}
}
Err(e)
}
}
Expand Down

0 comments on commit b565231

Please sign in to comment.