Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iroh-bytes): add more context to errors #2196

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions iroh-bytes/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;

use anyhow::{Context, Result};
use bao_tree::io::fsm::{encode_ranges_validated, Outboard};
use bao_tree::io::EncodeError;
use futures::future::BoxFuture;
use iroh_base::rpc::RpcError;
use iroh_io::stats::{
Expand Down Expand Up @@ -487,34 +488,50 @@ pub enum SentStatus {
NotFound,
}

/// Send a
/// Send a blob to the client.
pub async fn send_blob<D: Map, W: AsyncStreamWriter>(
db: &D,
name: Hash,
hash: Hash,
ranges: &RangeSpec,
mut writer: W,
) -> Result<(SentStatus, u64, SliceReaderStats)> {
match db.get(&name).await? {
match db.get(&hash).await? {
Some(entry) => {
let outboard = entry.outboard().await?;
let size = outboard.tree().size();
let mut file_reader = TrackingSliceReader::new(entry.data_reader().await?);
writer.write(size.to_le_bytes().as_slice()).await?;
let res = encode_ranges_validated(
encode_ranges_validated(
&mut file_reader,
outboard,
&ranges.to_chunk_ranges(),
writer,
)
.await;
debug!("done sending blob {} {:?}", name, res);
res?;
.await
.map_err(|e| encode_error_to_anyhow(e, &hash))?;

Ok((SentStatus::Sent, size, file_reader.stats()))
}
_ => {
debug!("blob not found {}", hex::encode(name));
debug!("blob not found {}", hash.to_hex());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually it's better to do these as tracing fields:

Suggested change
debug!("blob not found {}", hash.to_hex());
debug!(hash = %hash.to_hex(), "blob not found");

Ok((SentStatus::NotFound, 0, SliceReaderStats::default()))
}
}
}

fn encode_error_to_anyhow(err: EncodeError, hash: &Hash) -> anyhow::Error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because anyhow makes it particularly hard to downcast the hash (and rightly so, you probably need thiserror if that's what you want to support) another approach - which I usually take - is to add a tracing span with the hash because it's purely a reporting thing now. You can do an #[instrument(...)] to send_blob but probably also want to start the span even before this.

match err {
EncodeError::LeafHashMismatch(x) => anyhow::Error::from(EncodeError::LeafHashMismatch(x))
.context(format!("hash {} offset {}", hash.to_hex(), x.to_bytes())),
EncodeError::ParentHashMismatch(n) => {
let r = n.chunk_range();
anyhow::Error::from(EncodeError::ParentHashMismatch(n)).context(format!(
"hash {} range {}..{}",
hash.to_hex(),
r.start.to_bytes(),
r.end.to_bytes()
))
}
e => anyhow::Error::from(e).context(format!("hash {}", hash.to_hex())),
}
}
Loading