Slightly more detailed error reporting from helper

This commit is contained in:
Alex 2022-05-12 17:10:25 +02:00
parent ed76893581
commit e7ddba53e3
Signed by: lx
GPG key ID: 0E496D15096376BE
5 changed files with 22 additions and 15 deletions

View file

@ -84,6 +84,10 @@ pub enum Error {
#[error(display = "Invalid base64: {}", _0)] #[error(display = "Invalid base64: {}", _0)]
InvalidBase64(#[error(source)] base64::DecodeError), InvalidBase64(#[error(source)] base64::DecodeError),
/// Bucket name is not valid according to AWS S3 specs
#[error(display = "Invalid bucket name")]
InvalidBucketName,
/// The client sent invalid XML data /// The client sent invalid XML data
#[error(display = "Invalid XML: {}", _0)] #[error(display = "Invalid XML: {}", _0)]
InvalidXml(String), InvalidXml(String),
@ -126,6 +130,7 @@ impl From<HelperError> for Error {
match err { match err {
HelperError::Internal(i) => Self::InternalError(i), HelperError::Internal(i) => Self::InternalError(i),
HelperError::BadRequest(b) => Self::BadRequest(b), HelperError::BadRequest(b) => Self::BadRequest(b),
e => Self::BadRequest(format!("{}", e)),
} }
} }
} }

View file

@ -142,6 +142,7 @@ async fn cli_command(opt: Opt) -> Result<(), Error> {
match cli_command_dispatch(opt.cmd, &system_rpc_endpoint, &admin_rpc_endpoint, id).await { match cli_command_dispatch(opt.cmd, &system_rpc_endpoint, &admin_rpc_endpoint, id).await {
Err(HelperError::Internal(i)) => Err(Error::Message(format!("Internal error: {}", i))), Err(HelperError::Internal(i)) => Err(Error::Message(format!("Internal error: {}", i))),
Err(HelperError::BadRequest(b)) => Err(Error::Message(b)), Err(HelperError::BadRequest(b)) => Err(Error::Message(b)),
Err(e) => Err(Error::Message(format!("{}", e))),
Ok(x) => Ok(x), Ok(x) => Ok(x),
} }
} }

View file

@ -71,10 +71,7 @@ impl<'a> BucketHelper<'a> {
.get(&EmptyKey, &bucket_id) .get(&EmptyKey, &bucket_id)
.await? .await?
.filter(|b| !b.is_deleted()) .filter(|b| !b.is_deleted())
.ok_or_bad_request(format!( .ok_or_else(|| Error::NoSuchBucket(hex::encode(bucket_id)))
"Bucket {:?} does not exist or has been deleted",
bucket_id
))
} }
/// Sets a new alias for a bucket in global namespace. /// Sets a new alias for a bucket in global namespace.
@ -88,10 +85,7 @@ impl<'a> BucketHelper<'a> {
alias_name: &String, alias_name: &String,
) -> Result<(), Error> { ) -> Result<(), Error> {
if !is_valid_bucket_name(alias_name) { if !is_valid_bucket_name(alias_name) {
return Err(Error::BadRequest(format!( return Err(Error::InvalidBucketName(alias_name.to_string()));
"{}: {}",
alias_name, INVALID_BUCKET_NAME_MESSAGE
)));
} }
let mut bucket = self.get_existing_bucket(bucket_id).await?; let mut bucket = self.get_existing_bucket(bucket_id).await?;
@ -122,7 +116,7 @@ impl<'a> BucketHelper<'a> {
let alias = match alias { let alias = match alias {
None => BucketAlias::new(alias_name.clone(), alias_ts, Some(bucket_id)) None => BucketAlias::new(alias_name.clone(), alias_ts, Some(bucket_id))
.ok_or_bad_request(format!("{}: {}", alias_name, INVALID_BUCKET_NAME_MESSAGE))?, .ok_or_else(|| Error::InvalidBucketName(alias_name.clone()))?,
Some(mut a) => { Some(mut a) => {
a.state = Lww::raw(alias_ts, Some(bucket_id)); a.state = Lww::raw(alias_ts, Some(bucket_id));
a a
@ -210,7 +204,7 @@ impl<'a> BucketHelper<'a> {
.bucket_alias_table .bucket_alias_table
.get(&EmptyKey, alias_name) .get(&EmptyKey, alias_name)
.await? .await?
.ok_or_message(format!("Alias {} not found", alias_name))?; .ok_or_else(|| Error::NoSuchBucket(alias_name.to_string()))?;
// Checks ok, remove alias // Checks ok, remove alias
let alias_ts = match bucket.state.as_option() { let alias_ts = match bucket.state.as_option() {
@ -252,10 +246,7 @@ impl<'a> BucketHelper<'a> {
let key_helper = KeyHelper(self.0); let key_helper = KeyHelper(self.0);
if !is_valid_bucket_name(alias_name) { if !is_valid_bucket_name(alias_name) {
return Err(Error::BadRequest(format!( return Err(Error::InvalidBucketName(alias_name.to_string()));
"{}: {}",
alias_name, INVALID_BUCKET_NAME_MESSAGE
)));
} }
let mut bucket = self.get_existing_bucket(bucket_id).await?; let mut bucket = self.get_existing_bucket(bucket_id).await?;

View file

@ -10,6 +10,16 @@ pub enum Error {
#[error(display = "Bad request: {}", _0)] #[error(display = "Bad request: {}", _0)]
BadRequest(String), BadRequest(String),
/// Bucket name is not valid according to AWS S3 specs
#[error(display = "Invalid bucket name: {}", _0)]
InvalidBucketName(String),
#[error(display = "Access key not found: {}", _0)]
NoSuchAccessKey(String),
#[error(display = "Bucket not found: {}", _0)]
NoSuchBucket(String),
} }
impl From<netapp::error::Error> for Error { impl From<netapp::error::Error> for Error {

View file

@ -34,7 +34,7 @@ impl<'a> KeyHelper<'a> {
.get(&EmptyKey, key_id) .get(&EmptyKey, key_id)
.await? .await?
.filter(|b| !b.state.is_deleted()) .filter(|b| !b.state.is_deleted())
.ok_or_bad_request(format!("Key {} does not exist or has been deleted", key_id)) .ok_or_else(|| Error::NoSuchAccessKey(key_id.to_string()))
} }
/// Returns a Key if it is present in key table, /// Returns a Key if it is present in key table,