Skip to content

Commit

Permalink
fix: Improve error handling (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
faassen authored Oct 21, 2022
1 parent 6de85c9 commit 7a945ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 28 additions & 2 deletions iroh/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Parser;
use std::io;

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
Expand All @@ -10,5 +11,30 @@ async fn main() -> Result<()> {
// fixtures is invoked.
// Without the `testing` feature, the version of
// `run` that interacts with the real Iroh API is used.
cli.run().await
let r = cli.run().await;
let r = transform_error(r);
match r {
Ok(_) => Ok(()),
Err(e) => {
eprintln!("Error: {:?}", e);
std::process::exit(1);
}
}
}

fn transform_error(r: Result<()>) -> Result<()> {
match r {
Ok(_) => Ok(()),
Err(e) => {
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 Err(anyhow!(
"Connection refused. Are `iroh-p2p` and `iroh-store` running?"
));
}
}
Err(e)
}
}
}
4 changes: 2 additions & 2 deletions iroh/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ async fn add(api: &impl Api, path: &Path, no_wrap: bool, recursive: bool) -> Res
pb.inc(0);

let mut progress = api.add_stream(path, !no_wrap).await?;
while let Some(Ok(add_event)) = progress.next().await {
match add_event {
while let Some(add_event) = progress.next().await {
match add_event? {
AddEvent::ProgressDelta(size) => {
pb.inc(size);
}
Expand Down

0 comments on commit 7a945ed

Please sign in to comment.