From eb3f2ac527839f5ede67a3d7f01a56cbf1b9c4e6 Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Wed, 27 Sep 2023 18:05:53 +0530 Subject: [PATCH] Slightly better errors --- src/commands/download.rs | 2 +- src/commands/upload.rs | 10 +++++++--- src/lib.rs | 22 ++++++++++++++++++---- src/main.rs | 22 +++++++++++++++------- src/utils.rs | 10 ++++++++-- src/vercel/artifact.rs | 2 +- 6 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/commands/download.rs b/src/commands/download.rs index 411e1d0..749f002 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,5 +1,5 @@ pub async fn download( - remote_client: vercel_cache_helper::vercel::remote_cache_client::RemoteClient + remote_client: vercel_cache_helper::vercel::remote_cache_client::RemoteClient, ) -> vercel_cache_helper::Result<()> { Ok(()) } diff --git a/src/commands/upload.rs b/src/commands/upload.rs index 33b9e03..2d04757 100644 --- a/src/commands/upload.rs +++ b/src/commands/upload.rs @@ -34,7 +34,7 @@ pub async fn upload( println!("{:?}", build_dir_archive.metadata()); - let build_archive_size = build_dir_archive.read_to_end(&mut build_archive_buf)?; + let build_archive_size = build_dir_archive.read_to_end(&mut build_archive_buf)?; let cache_archive_size = cache_dir_archive.read_to_end(&mut cache_archive_buf)?; println!("Build archive bytes read: {} bytes", build_archive_size); @@ -48,11 +48,15 @@ pub async fn upload( let mut build_put_req = remote_client.put(build_archive_hash, None)?; println!("Uploading build"); - build_put_req.buffer(&mut build_archive_buf, build_archive_size).await?; + build_put_req + .buffer(&mut build_archive_buf, build_archive_size) + .await?; let mut cache_put_req = remote_client.put(cache_dir_hash, None)?; println!("Uploading Cache"); - let res = cache_put_req.buffer(&mut cache_archive_buf, cache_archive_size).await?; + let res = cache_put_req + .buffer(&mut cache_archive_buf, cache_archive_size) + .await?; println!("Response: {:?}", res); println!("Response body: {:?}", res.text().await?); diff --git a/src/lib.rs b/src/lib.rs index 2f24bed..8a93410 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,19 +6,27 @@ use std::convert::From; pub enum Error { FileNotFound(String), InvalidInput(String), + EnvVarNotFound(String), + ReqwestError(String), + Other(Box), } -impl std::error::Error for Error {} - impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Error::FileNotFound(message) => write!(f, "File not found: {}", message), Error::InvalidInput(message) => write!(f, "Invalid input: {}", message), + Error::EnvVarNotFound(var_name) => { + write!(f, "Environment variable not found: {}", var_name) + } + Error::ReqwestError(message) => write!(f, "Reqwest error: {}", message), + Error::Other(inner) => write!(f, "Other error: {}", inner), } } } +impl std::error::Error for Error {} + pub type Result = std::result::Result; impl From for Error { @@ -27,9 +35,15 @@ impl From for Error { } } +impl From for Error { + fn from(env_error: std::env::VarError) -> Self { + Error::EnvVarNotFound(env_error.to_string()) + } +} + impl From for Error { fn from(reqwest_error: reqwest::Error) -> Self { - Error::InvalidInput(reqwest_error.to_string()) + Error::ReqwestError(reqwest_error.to_string()) } } diff --git a/src/main.rs b/src/main.rs index ee3e80f..30ded28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,19 +14,25 @@ enum Commands { } #[tokio::main] -async fn main() { +async fn main() -> vercel_cache_helper::Result<()> { let cli = Cli::parse(); - let token = std::env::var("VERCEL_TOKEN").expect("Vercel token not found"); + let token = std::env::var("VERCEL_TOKEN").map_err(|_e| { + vercel_cache_helper::Error::EnvVarNotFound("Vercel token not found".to_string()) + })?; let team_id = std::env::var("VERCEL_TEAM_ID").unwrap_or("".to_string()); - let product = std::env::var("VERCEL_PRODUCT").expect("Vercel product name not found"); + let product = std::env::var("VERCEL_PRODUCT").map_err(|_e| { + vercel_cache_helper::Error::EnvVarNotFound("Vercel product name not found".to_string()) + })?; let remote_client = vercel_cache_helper::get_remote_client(token, Some(team_id), product); - let result_future: std::pin::Pin>>> = match &cli.command { - Some(Commands::Download {}) => { - Box::pin(vercel_cache_helper::commands::download::download(remote_client)) - } + let result_future: std::pin::Pin< + Box>>, + > = match &cli.command { + Some(Commands::Download {}) => Box::pin(vercel_cache_helper::commands::download::download( + remote_client, + )), Some(Commands::Upload {}) => { Box::pin(vercel_cache_helper::commands::upload::upload(remote_client)) } @@ -35,4 +41,6 @@ async fn main() { // Now you can await the result_future if necessary let _ = result_future.await; + + Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index 1f513e9..bdfc807 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -39,7 +39,10 @@ pub fn generate_file_hash(mut file: std::fs::File) -> vercel_cache_helper::Resul Ok(generate_hash(buf)) } -pub fn create_tar_gz_archive(src_folder: &std::path::PathBuf, dest_file: &std::fs::File) -> vercel_cache_helper::Result<()> { +pub fn create_tar_gz_archive( + src_folder: &std::path::PathBuf, + dest_file: &std::fs::File, +) -> vercel_cache_helper::Result<()> { println!("Creating archive from: {:?}", src_folder); if !src_folder.exists() { @@ -52,7 +55,10 @@ pub fn create_tar_gz_archive(src_folder: &std::path::PathBuf, dest_file: &std::f return Ok(()); } - let gz_encoder = std::io::BufWriter::new(flate2::write::GzEncoder::new(dest_file, flate2::Compression::default())); + let gz_encoder = std::io::BufWriter::new(flate2::write::GzEncoder::new( + dest_file, + flate2::Compression::default(), + )); let mut tar_builder = tar::Builder::new(gz_encoder); tar_builder.append_dir_all("", src_folder)?; diff --git a/src/vercel/artifact.rs b/src/vercel/artifact.rs index 3835cb7..3b4e8a5 100644 --- a/src/vercel/artifact.rs +++ b/src/vercel/artifact.rs @@ -125,7 +125,7 @@ impl ArtifactPutRequest { pub async fn buffer( &mut self, artifact: &mut [u8], - content_len: usize + content_len: usize, ) -> vercel_cache_helper::Result { let client = reqwest::Client::new();