Skip to content

Commit

Permalink
return underlying err from download_with_retry
Browse files Browse the repository at this point in the history
  • Loading branch information
bluegenes committed May 9, 2024
1 parent bc893da commit a84b98a
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/directsketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ async fn download_with_retry(
retry_count: u32,
) -> Result<Vec<u8>> {
let mut attempts = retry_count;
let mut last_error: Option<anyhow::Error> = None;

while attempts > 0 {
let response = client.get(url).send().await;
Expand All @@ -195,17 +196,25 @@ async fn download_with_retry(
if computed_hash == md5 {
return Ok(data.to_vec());
} else {
eprintln!(
"MD5 hash does not match. Expected: {}, Found: {}. Retrying...",
md5, computed_hash
);
last_error = Some(anyhow!(
"MD5 hash does not match. Expected: {}, Found: {}",
md5,
computed_hash
));
}
} else {
return Ok(data.to_vec()); // If no expected MD5 is provided, just return the data
}
}
_ => {
eprintln!("Failed to download file: {}. Retrying...", url);
Ok(resp) => {
last_error = Some(anyhow!(
"Server error status code {}: {}. Retrying...",
resp.status(),
url
));
}
Err(e) => {
last_error = Some(anyhow!("Failed to download file: {}. Error: {}.", url, e));
}
}

Expand All @@ -214,12 +223,13 @@ async fn download_with_retry(
break;
}
}

Err(anyhow!(
"Failed to download file after {} retries: {}",
retry_count,
url
))
Err(last_error.unwrap_or_else(|| {
anyhow!(
"Failed to download file after {} retries: {}",
url,
retry_count
)
}))
}

async fn sketch_data(
Expand Down

0 comments on commit a84b98a

Please sign in to comment.