diff --git a/noir-libs/src/network.rs b/noir-libs/src/network.rs index 53aae68..467607a 100644 --- a/noir-libs/src/network.rs +++ b/noir-libs/src/network.rs @@ -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. @@ -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()); diff --git a/noir-libs/src/ops/add.rs b/noir-libs/src/ops/add.rs index 029afac..6eb4542 100644 --- a/noir-libs/src/ops/add.rs +++ b/noir-libs/src/ops/add.rs @@ -14,15 +14,20 @@ pub fn add(package_name: &str, version: &str) -> Result { 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); @@ -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( @@ -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 { 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.