From 8e5c238078e9a14189d1f8862a6278a8e8ba3908 Mon Sep 17 00:00:00 2001 From: "Franz Heinzmann (Frando)" Date: Tue, 21 May 2024 11:02:55 +0200 Subject: [PATCH] better error messages --- iroh/src/docs_engine.rs | 27 ++++++++++++++++++++++----- iroh/src/node.rs | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/iroh/src/docs_engine.rs b/iroh/src/docs_engine.rs index 389526833f5..b64870fda32 100644 --- a/iroh/src/docs_engine.rs +++ b/iroh/src/docs_engine.rs @@ -9,7 +9,7 @@ use std::{ sync::{Arc, RwLock}, }; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use futures_lite::{Stream, StreamExt}; use iroh_blobs::downloader::Downloader; use iroh_blobs::{store::EntryStatus, Hash}; @@ -317,8 +317,18 @@ impl DefaultAuthorStorage { } Self::Persistent(ref path) => { if path.exists() { - let data = tokio::fs::read_to_string(path).await?; - let author_id = AuthorId::from_str(&data)?; + let data = tokio::fs::read_to_string(path).await.with_context(|| { + format!( + "Failed to read the default author file at `{}`", + path.to_string_lossy() + ) + })?; + let author_id = AuthorId::from_str(&data).with_context(|| { + format!( + "Failed to parse the default author from `{}`", + path.to_string_lossy() + ) + })?; if docs_store.export_author(author_id).await?.is_none() { bail!("The default author is missing from the docs store. To recover, delete the file `{}`. Then iroh will create a new default author.", path.to_string_lossy()) } @@ -327,7 +337,7 @@ impl DefaultAuthorStorage { let author = Author::new(&mut rand::thread_rng()); let author_id = author.id(); docs_store.import_author(author).await?; - tokio::fs::write(path, author_id.to_string()).await?; + self.persist(author_id).await?; Ok(author_id) } } @@ -339,7 +349,14 @@ impl DefaultAuthorStorage { // persistence is not possible for the mem storage so this is a noop. } Self::Persistent(ref path) => { - tokio::fs::write(path, author_id.to_string()).await?; + tokio::fs::write(path, author_id.to_string()) + .await + .with_context(|| { + format!( + "Failed to write the default author to `{}`", + path.to_string_lossy() + ) + })?; } } Ok(()) diff --git a/iroh/src/node.rs b/iroh/src/node.rs index 9bbf3ffc5b6..07a3d54ab32 100644 --- a/iroh/src/node.rs +++ b/iroh/src/node.rs @@ -487,7 +487,12 @@ mod tests { // check that the default author exists and cannot be deleted. let default_author = { - let iroh = Node::persistent(iroh_root).await.unwrap().spawn().await.unwrap(); + let iroh = Node::persistent(iroh_root) + .await + .unwrap() + .spawn() + .await + .unwrap(); let author = iroh.authors.default().await.unwrap(); assert!(iroh.authors.export(author).await.unwrap().is_some()); assert!(iroh.authors.delete(author).await.is_err()); @@ -497,7 +502,12 @@ mod tests { // check that the default author is persisted across restarts. { - let iroh = Node::persistent(iroh_root).await.unwrap().spawn().await.unwrap(); + let iroh = Node::persistent(iroh_root) + .await + .unwrap() + .spawn() + .await + .unwrap(); let author = iroh.authors.default().await.unwrap(); assert_eq!(author, default_author); assert!(iroh.authors.export(author).await.unwrap().is_some()); @@ -511,7 +521,12 @@ mod tests { tokio::fs::remove_file(IrohPaths::DefaultAuthor.with_root(iroh_root)) .await .unwrap(); - let iroh = Node::persistent(iroh_root).await.unwrap().spawn().await.unwrap(); + let iroh = Node::persistent(iroh_root) + .await + .unwrap() + .spawn() + .await + .unwrap(); let author = iroh.authors.default().await.unwrap(); assert!(author != default_author); assert!(iroh.authors.export(author).await.unwrap().is_some()); @@ -542,7 +557,12 @@ mod tests { // check that the default author can be set manually and is persisted. let default_author = { - let iroh = Node::persistent(iroh_root).await.unwrap().spawn().await.unwrap(); + let iroh = Node::persistent(iroh_root) + .await + .unwrap() + .spawn() + .await + .unwrap(); let author = iroh.authors.create().await.unwrap(); iroh.authors.set_default(author).await.unwrap(); assert_eq!(iroh.authors.default().await.unwrap(), author); @@ -550,7 +570,12 @@ mod tests { author }; { - let iroh = Node::persistent(iroh_root).await.unwrap().spawn().await.unwrap(); + let iroh = Node::persistent(iroh_root) + .await + .unwrap() + .spawn() + .await + .unwrap(); assert_eq!(iroh.authors.default().await.unwrap(), default_author); iroh.shutdown().await.unwrap(); }