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

misc: add not found error to the fetch repo data errors #256

Merged
merged 6 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions crates/rattler-bin/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rattler_repodata_gateway::fetch::{
};
use rattler_repodata_gateway::sparse::SparseRepoData;
use rattler_solve::{libsolv_c, libsolv_rs, SolverImpl, SolverTask};
use reqwest::{Client, StatusCode};
use reqwest::Client;
use std::{
borrow::Cow,
env,
Expand Down Expand Up @@ -591,9 +591,7 @@ async fn fetch_repo_data_records_with_progress(
// Error out if an error occurred, but also update the progress bar
let result = match result {
Err(e) => {
let not_found = matches!(&e,
FetchRepoDataError::HttpError(e) if e.status() == Some(StatusCode::NOT_FOUND)
);
let not_found = matches!(&e, FetchRepoDataError::NotFound(_));
if not_found && platform != Platform::NoArch {
progress_bar.set_style(finished_progress_style());
progress_bar.finish_with_message("Not Found");
Expand Down
103 changes: 95 additions & 8 deletions crates/rattler_repodata_gateway/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ use url::Url;
mod cache;
pub mod jlap;

/// RepoData could not be found for given channel and platform
#[derive(Debug, thiserror::Error)]
pub enum RepoDataNotFoundError {
/// There was an error on the Http request
#[error(transparent)]
HttpError(#[from] reqwest::Error),

/// There was a file system error
#[error(transparent)]
FileSystemError(std::io::Error),
}

#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
pub enum FetchRepoDataError {
Expand All @@ -36,6 +48,9 @@ pub enum FetchRepoDataError {
#[error(transparent)]
FailedToDownloadRepoData(std::io::Error),

#[error("repodata not found")]
NotFound(#[source] RepoDataNotFoundError),
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved

#[error("failed to create temporary file for repodata.json")]
FailedToCreateTemporaryFile(#[source] std::io::Error),

Expand Down Expand Up @@ -67,6 +82,12 @@ impl From<tokio::task::JoinError> for FetchRepoDataError {
}
}

impl From<RepoDataNotFoundError> for FetchRepoDataError {
fn from(err: RepoDataNotFoundError) -> Self {
FetchRepoDataError::NotFound(err)
}
}

ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
/// Defines how to use the repodata cache.
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
pub enum CacheAction {
Expand Down Expand Up @@ -187,14 +208,14 @@ async fn repodata_from_file(
// copy file from subdir_url to out_path
tokio::fs::copy(&subdir_url.to_file_path().unwrap(), &out_path)
.await
.map_err(FetchRepoDataError::FailedToDownloadRepoData)?;
.map_err(RepoDataNotFoundError::FileSystemError)?;
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved

// create a dummy cache state
let new_cache_state = RepoDataState {
url: subdir_url.clone(),
cache_size: tokio::fs::metadata(&out_path)
.await
.map_err(FetchRepoDataError::FailedToDownloadRepoData)?
.map_err(RepoDataNotFoundError::FileSystemError)?
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
.len(),
cache_headers: CacheHeaders {
etag: None,
Expand Down Expand Up @@ -440,11 +461,24 @@ pub async fn fetch_repo_data(
cache_headers.add_to_request(&mut headers)
}
// Send the request and wait for a reply
let response = request_builder
.headers(headers)
.send()
.await?
.error_for_status()?;
let result = request_builder.headers(headers).send().await;
let response = match result {
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
Ok(response) if response.status() == StatusCode::NOT_FOUND => {
return Err(FetchRepoDataError::NotFound(
RepoDataNotFoundError::HttpError(response.error_for_status().unwrap_err()),
));
}
Ok(response) => {
if response.status().is_success() {
response
} else {
response.error_for_status()?
}
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
}
Err(e) => {
return Err(FetchRepoDataError::HttpError(e));
}
};

// If the content didn't change, simply return whatever we have on disk.
if response.status() == StatusCode::NOT_MODIFIED {
Expand Down Expand Up @@ -613,7 +647,7 @@ async fn stream_and_decode_to_file(
// Decode, hash and write the data to the file.
let bytes = tokio::io::copy(&mut decoded_repo_data_json_bytes, &mut hashing_file_writer)
.await
.map_err(FetchRepoDataError::FailedToDownloadRepoData)?;
.map_err(RepoDataNotFoundError::FileSystemError)?;

ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
// Finalize the hash
let (_, hash) = hashing_file_writer.finalize();
Expand Down Expand Up @@ -971,6 +1005,7 @@ mod test {
use super::{
fetch_repo_data, CacheResult, CachedRepoData, DownloadProgress, FetchRepoDataOptions,
};
use crate::fetch::{FetchRepoDataError, RepoDataNotFoundError};
use crate::utils::simple_channel_server::SimpleChannelServer;
use crate::utils::Encoding;
use assert_matches::assert_matches;
Expand Down Expand Up @@ -1348,4 +1383,56 @@ mod test {

assert_eq!(last_download_progress.load(Ordering::SeqCst), 1110);
}

#[tracing_test::traced_test]
#[tokio::test]
pub async fn test_repodata_not_found() {
// Create a directory with some repodata.
let subdir_path = TempDir::new().unwrap();
// Don't add repodata to the channel.

// Download the "data" from the local filebased channel.
let cache_dir = TempDir::new().unwrap();
let result = fetch_repo_data(
Url::parse(format!("file://{}", subdir_path.path().to_str().unwrap()).as_str())
.unwrap(),
AuthenticatedClient::default(),
cache_dir.path(),
FetchRepoDataOptions {
..Default::default()
},
)
.await;

assert!(result.is_err());
assert!(matches!(
result,
Err(FetchRepoDataError::NotFound(
RepoDataNotFoundError::FileSystemError(_)
))
));

// Start a server to test the http error
let server = SimpleChannelServer::new(subdir_path.path());

// Download the "data" from the channel.
let cache_dir = TempDir::new().unwrap();
let result = fetch_repo_data(
server.url(),
AuthenticatedClient::default(),
cache_dir.path(),
FetchRepoDataOptions {
..Default::default()
},
)
.await;

assert!(result.is_err());
assert!(matches!(
result,
Err(FetchRepoDataError::NotFound(
RepoDataNotFoundError::HttpError(_)
))
));
}
}