Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed May 21, 2024
1 parent 8a187a9 commit 8e5c238
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
27 changes: 22 additions & 5 deletions iroh/src/docs_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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())
}
Expand All @@ -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)
}
}
Expand All @@ -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(())
Expand Down
35 changes: 30 additions & 5 deletions iroh/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -542,15 +557,25 @@ 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);
iroh.shutdown().await.unwrap();
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();
}
Expand Down

0 comments on commit 8e5c238

Please sign in to comment.