Skip to content

Commit

Permalink
refactor: Simplify errors now that iroh_car::Error is exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Sep 4, 2023
1 parent ab54a35 commit ab1576e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
22 changes: 4 additions & 18 deletions car-mirror/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(unknown_lints)] // Because the `instrument` macro contains some `#[allow]`s that rust 1.66 doesn't know yet.

use anyhow::anyhow;
use bytes::Bytes;
use deterministic_bloom::runtime_size::BloomFilter;
use futures::TryStreamExt;
Expand Down Expand Up @@ -115,11 +114,7 @@ pub async fn block_send(
.await?;

Ok(CarFile {
bytes: writer
.finish()
.await
.map_err(|e| Error::CarFileError(anyhow!(e)))?
.into(),
bytes: writer.finish().await?.into(),
})
}

Expand All @@ -142,9 +137,7 @@ pub async fn block_receive(
let mut dag_verification = IncrementalDagVerification::new([root], store, cache).await?;

if let Some(car) = last_car {
let mut reader = CarReader::new(Cursor::new(car.bytes))
.await
.map_err(|e| Error::CarFileError(anyhow!(e)))?;
let mut reader = CarReader::new(Cursor::new(car.bytes)).await?;

read_and_verify_blocks(
&mut dag_verification,
Expand Down Expand Up @@ -305,10 +298,7 @@ async fn write_blocks_into_car<W: tokio::io::AsyncWrite + Unpin + Send>(
"writing block to CAR",
);

block_bytes += writer
.write(cid, &block)
.await
.map_err(|e| Error::CarFileError(anyhow!(e)))?;
block_bytes += writer.write(cid, &block).await?;

if block_bytes > send_minimum {
break;
Expand All @@ -326,11 +316,7 @@ async fn read_and_verify_blocks<R: tokio::io::AsyncRead + Unpin>(
cache: &impl Cache,
) -> Result<(), Error> {
let mut bytes_read = 0;
while let Some((cid, vec)) = reader
.next_block()
.await
.map_err(|e| Error::CarFileError(anyhow!(e)))?
{
while let Some((cid, vec)) = reader.next_block().await? {
let block = Bytes::from(vec);

debug!(
Expand Down
8 changes: 4 additions & 4 deletions car-mirror/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ pub enum Error {
#[error("Error during block parsing: {0}")]
ParsingError(anyhow::Error),

/// An error rasied when trying to read or write a CAR file.
#[error("CAR (de)serialization error: {0}")]
CarFileError(anyhow::Error),

/// An error rasied from the blockstore.
#[error("BlockStore error: {0}")]
BlockStoreError(anyhow::Error),
Expand All @@ -64,6 +60,10 @@ pub enum Error {
/// Errors related to incremental verification
#[error(transparent)]
IncrementalVerificationError(#[from] IncrementalVerificationError),

/// An error rasied when trying to read or write a CAR file.
#[error("CAR (de)serialization error: {0}")]
CarFileError(#[from] iroh_car::Error),
}

/// Errors related to incremental verification
Expand Down

0 comments on commit ab1576e

Please sign in to comment.