-
Notifications
You must be signed in to change notification settings - Fork 165
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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::{ | ||
|
@@ -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()); | ||
Ok((SentStatus::NotFound, 0, SliceReaderStats::default())) | ||
} | ||
} | ||
} | ||
|
||
fn encode_error_to_anyhow(err: EncodeError, hash: &Hash) -> anyhow::Error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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())), | ||
} | ||
} |
There was a problem hiding this comment.
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: