Skip to content

Commit

Permalink
Bubbling errors for missing package
Browse files Browse the repository at this point in the history
  • Loading branch information
microbecode committed Jan 6, 2025
1 parent adb8103 commit ee8ff42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
22 changes: 16 additions & 6 deletions noir-libs/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ use std::path::Path;
///
/// This function will panic if the request fails, if the file cannot be created,
/// or if writing to the file fails.
pub fn download_remote(output_path: &Path, url: &str) {
//println!("Downloading package from url {}", url);
pub fn download_remote(output_path: &Path, url: &str) -> Result<(), String> {
let mut response = get(url).map_err(|e| e.to_string())?;
//println!("RESPONSE {:?}", response);

// Check if the response status is successful (200 OK)
if !response.status().is_success() {
return Err(format!(
"Failed to download file: Received status code {}",
response.status()
));
}

let mut dest = File::create(output_path).map_err(|e| e.to_string())?;
copy(&mut response, &mut dest).map_err(|e| e.to_string())?;

let mut response = get(url).expect("Failed to send request");
let mut dest = File::create(output_path).expect("Failed to create file");
copy(&mut response, &mut dest).expect("Failed to write to file");
Ok(())
}

/// Retrieves the latest version of a package from the specified URL.
Expand Down Expand Up @@ -83,7 +93,7 @@ mod tests {
let temp_dir = tempdir().expect("Failed to create temp dir");
let output_path = temp_dir.path().join("test_file");
let url_str = format!("{}/hello", url);
download_remote(&output_path, &url_str);
download_remote(&output_path, &url_str).unwrap();

assert!(output_path.is_file());

Expand Down
23 changes: 15 additions & 8 deletions noir-libs/src/ops/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ pub fn add(package_name: &str, version: &str) -> Result<String, String> {

let used_version = get_used_version(package_name, version)?;

store_package(cache_root.clone(), pwd.clone(), package_name, &used_version);
store_package(cache_root.clone(), pwd.clone(), package_name, &used_version)?;
add_dep_to_manifest(pwd, cache_root, package_name, &used_version);

Ok(used_version)
}

fn store_package(cache_root: PathBuf, project_dir: PathBuf, package_name: &str, version: &str) {
fn store_package(
cache_root: PathBuf,
project_dir: PathBuf,
package_name: &str,
version: &str,
) -> Result<(), String> {
// Get the package into the cache
let package_cache_path = get_to_cache(cache_root.clone(), package_name, version);
let package_cache_path = get_to_cache(cache_root.clone(), package_name, version)?;

// Resolve sub-dependencies (if any)
let package_manifest_path = package_cache_path.join(MANIFEST_FILE_NAME);
Expand All @@ -37,9 +42,10 @@ fn store_package(cache_root: PathBuf, project_dir: PathBuf, package_name: &str,
project_dir.clone(),
&sub_dep_name,
&sub_dep_version,
);
)?;
}
}
Ok(())
}

fn add_dep_to_manifest(
Expand Down Expand Up @@ -71,16 +77,17 @@ fn add_dep_to_manifest(
/// # Returns
///
/// Returns the path to the cached package.
fn get_to_cache(cache_root: PathBuf, package_name: &str, version: &str) -> PathBuf {
fn get_to_cache(cache_root: PathBuf, package_name: &str, version: &str) -> Result<PathBuf, String> {
let package_storage = get_cache_storage(cache_root.clone(), package_name, version);
let cached_package_path = get_package_dir(cache_root, package_name, version);

let url = get_package_url(package_name, version);

download_remote(&package_storage, &url);
extract_package(&package_storage, &cached_package_path).expect("Problem extracting package");
download_remote(&package_storage, &url)?;
extract_package(&package_storage, &cached_package_path)
.map_err(|_| "Problem extracting package".to_string())?;

cached_package_path
Ok(cached_package_path)
}

/// Retrieves the used version of a package.
Expand Down

0 comments on commit ee8ff42

Please sign in to comment.