Skip to content

Commit

Permalink
Slightly better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Sep 27, 2023
1 parent 1b138b8 commit eb3f2ac
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/commands/download.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
10 changes: 7 additions & 3 deletions src/commands/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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?);
Expand Down
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ use std::convert::From;
pub enum Error {
FileNotFound(String),
InvalidInput(String),
EnvVarNotFound(String),
ReqwestError(String),
Other(Box<dyn std::error::Error + Send + Sync>),
}

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<T> = std::result::Result<T, Error>;

impl From<std::io::Error> for Error {
Expand All @@ -27,9 +35,15 @@ impl From<std::io::Error> for Error {
}
}

impl From<std::env::VarError> for Error {
fn from(env_error: std::env::VarError) -> Self {
Error::EnvVarNotFound(env_error.to_string())
}
}

impl From<reqwest::Error> for Error {
fn from(reqwest_error: reqwest::Error) -> Self {
Error::InvalidInput(reqwest_error.to_string())
Error::ReqwestError(reqwest_error.to_string())
}
}

Expand Down
22 changes: 15 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn std::future::Future<Output = Result<(), vercel_cache_helper::Error>>>> = match &cli.command {
Some(Commands::Download {}) => {
Box::pin(vercel_cache_helper::commands::download::download(remote_client))
}
let result_future: std::pin::Pin<
Box<dyn std::future::Future<Output = Result<(), vercel_cache_helper::Error>>>,
> = 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))
}
Expand All @@ -35,4 +41,6 @@ async fn main() {

// Now you can await the result_future if necessary
let _ = result_future.await;

Ok(())
}
10 changes: 8 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion src/vercel/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<reqwest::Response> {
let client = reqwest::Client::new();

Expand Down

0 comments on commit eb3f2ac

Please sign in to comment.