Skip to content

Commit

Permalink
feat: report error message as part of Error::ListS3ObjectsError
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Sep 26, 2023
1 parent f50673c commit bfb0bac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub enum Error {
InquireError(#[from] inquire::InquireError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Failed to list objects in S3 bucket with '{0}' prefix")]
ListS3ObjectsError(String),
#[error("Failed to list objects in S3 bucket with prefix '{prefix}': {error}")]
ListS3ObjectsError { prefix: String, error: String },
#[error("Logs for a '{0}' testnet already exist")]
LogsForPreviousTestnetExist(String),
#[error("Logs have not been retrieved for the '{0}' environment.")]
Expand Down
30 changes: 14 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,27 +246,25 @@ async fn main() -> Result<()> {
let testnet_deploy = TestnetDeployBuilder::default()
.provider(provider.clone())
.build()?;
let result = testnet_deploy.init(&name).await;
match result {

match testnet_deploy.init(&name).await {
Ok(_) => {}
Err(e) => match e {
Error::LogsForPreviousTestnetExist(_) => {
return Err(eyre!(e)
.wrap_err(format!(
"Logs already exist for a previous testnet with the \
Err(e @ Error::LogsForPreviousTestnetExist(_)) => {
return Err(eyre!(e)
.wrap_err(format!(
"Logs already exist for a previous testnet with the \
name '{name}'"
))
.suggestion(
"If you wish to keep them, retrieve the logs with the 'logs get' \
))
.suggestion(
"If you wish to keep them, retrieve the logs with the 'logs get' \
command, then remove them with 'logs rm'. If you don't need them, \
simply run 'logs rm'. Then you can proceed with deploying your \
new testnet.",
));
}
_ => {
return Err(eyre!(e));
}
},
));
}
Err(e) => {
return Err(eyre!(e));
}
}

let logstash_deploy = LogstashDeployBuilder::default()
Expand Down
32 changes: 21 additions & 11 deletions src/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::error::{Error, Result};
use async_recursion::async_recursion;
use async_trait::async_trait;
use aws_sdk_s3::Client;
use aws_sdk_s3::{error::ProvideErrorMetadata, Client};
#[cfg(test)]
use mockall::automock;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -78,20 +78,24 @@ impl S3RepositoryInterface for S3Repository {

async fn folder_exists(&self, bucket_name: &str, folder_path: &str) -> Result<bool> {
let conf = aws_config::from_env().region("eu-west-2").load().await;

let client = Client::new(&conf);
let folder = if folder_path.ends_with('/') {
let prefix = if folder_path.ends_with('/') {
folder_path.to_string()
} else {
format!("{}/", folder_path)
};
let output = client
.list_objects_v2()
.bucket(bucket_name.to_string())
.prefix(folder)
.delimiter("/".to_string())
.bucket(bucket_name)
.prefix(&prefix)
.delimiter("/")
.send()
.await
.map_err(|_| Error::ListS3ObjectsError(folder_path.to_string()))?;
.map_err(|err| Error::ListS3ObjectsError {
prefix,
error: err.meta().message().unwrap_or_default().to_string(),
})?;
Ok(!output.contents().unwrap_or_default().is_empty())
}
}
Expand All @@ -109,10 +113,13 @@ impl S3Repository {
.list_objects_v2()
.bucket(bucket_name)
.prefix(prefix)
.delimiter("/".to_string())
.delimiter("/")
.send()
.await
.map_err(|_| Error::ListS3ObjectsError(prefix.to_string()))?;
.map_err(|err| Error::ListS3ObjectsError {
prefix: prefix.to_string(),
error: err.meta().message().unwrap_or_default().to_string(),
})?;

// So-called 'common prefixes' are subdirectories.
if let Some(common_prefixes) = output.common_prefixes {
Expand Down Expand Up @@ -149,12 +156,15 @@ impl S3Repository {
) -> Result<(), Error> {
let output = client
.list_objects_v2()
.bucket(bucket_name.to_string())
.bucket(bucket_name)
.prefix(prefix)
.delimiter("/".to_string())
.delimiter("/")
.send()
.await
.map_err(|_| Error::ListS3ObjectsError(prefix.to_string()))?;
.map_err(|err| Error::ListS3ObjectsError {
prefix: prefix.to_string(),
error: err.meta().message().unwrap_or_default().to_string(),
})?;

// So-called 'common prefixes' are subdirectories.
if let Some(common_prefixes) = output.common_prefixes {
Expand Down

0 comments on commit bfb0bac

Please sign in to comment.