Return BadRequest codes for some admin_rpc failure cases

This commit is contained in:
Alex 2020-04-21 16:45:32 +00:00
parent b1ddb933b0
commit 8915224966

View file

@ -49,7 +49,7 @@ impl AdminRpcHandler {
match msg { match msg {
AdminRPC::BucketOperation(bo) => self2.handle_bucket_cmd(bo).await, AdminRPC::BucketOperation(bo) => self2.handle_bucket_cmd(bo).await,
AdminRPC::LaunchRepair(opt) => self2.handle_launch_repair(opt).await, AdminRPC::LaunchRepair(opt) => self2.handle_launch_repair(opt).await,
_ => Err(Error::Message(format!("Invalid RPC"))), _ => Err(Error::BadRequest(format!("Invalid RPC"))),
} }
} }
}); });
@ -77,13 +77,16 @@ impl AdminRpcHandler {
.filter(|b| !b.deleted); .filter(|b| !b.deleted);
match bucket { match bucket {
Some(b) => Ok(AdminRPC::BucketInfo(b)), Some(b) => Ok(AdminRPC::BucketInfo(b)),
None => Err(Error::Message(format!("Bucket {} not found", query.name))), None => Err(Error::BadRequest(format!(
"Bucket {} not found",
query.name
))),
} }
} }
BucketOperation::Create(query) => { BucketOperation::Create(query) => {
let bucket = self.garage.bucket_table.get(&EmptyKey, &query.name).await?; let bucket = self.garage.bucket_table.get(&EmptyKey, &query.name).await?;
if bucket.as_ref().filter(|b| !b.deleted).is_some() { if bucket.as_ref().filter(|b| !b.deleted).is_some() {
return Err(Error::Message(format!( return Err(Error::BadRequest(format!(
"Bucket {} already exists", "Bucket {} already exists",
query.name query.name
))); )));
@ -112,7 +115,7 @@ impl AdminRpcHandler {
.filter(|b| !b.deleted) .filter(|b| !b.deleted)
{ {
None => { None => {
return Err(Error::Message(format!( return Err(Error::BadRequest(format!(
"Bucket {} does not exist", "Bucket {} does not exist",
query.name query.name
))); )));
@ -125,13 +128,13 @@ impl AdminRpcHandler {
.get_range(&query.name, None, Some(()), 10) .get_range(&query.name, None, Some(()), 10)
.await?; .await?;
if !objects.is_empty() { if !objects.is_empty() {
return Err(Error::Message(format!( return Err(Error::BadRequest(format!(
"Bucket {} is not empty", "Bucket {} is not empty",
query.name query.name
))); )));
} }
if !query.yes { if !query.yes {
return Err(Error::Message(format!( return Err(Error::BadRequest(format!(
"Add --yes flag to really perform this operation" "Add --yes flag to really perform this operation"
))); )));
} }
@ -155,7 +158,7 @@ impl AdminRpcHandler {
async fn handle_launch_repair(self: &Arc<Self>, opt: RepairOpt) -> Result<AdminRPC, Error> { async fn handle_launch_repair(self: &Arc<Self>, opt: RepairOpt) -> Result<AdminRPC, Error> {
if !opt.yes { if !opt.yes {
return Err(Error::Message(format!( return Err(Error::BadRequest(format!(
"Please provide the --yes flag to initiate repair operations." "Please provide the --yes flag to initiate repair operations."
))); )));
} }