mirror of
https://github.com/deuxfleurs-org/garage.git
synced 2026-08-20 18:12:15 +00:00
Refactor error management in API part
This commit is contained in:
+15
-17
@@ -9,8 +9,9 @@ use sha2::{Digest as Sha256Digest, Sha256};
|
||||
|
||||
use garage_table::*;
|
||||
use garage_util::data::*;
|
||||
use garage_util::error::Error;
|
||||
use garage_util::error::Error as GarageError;
|
||||
|
||||
use crate::error::*;
|
||||
use garage_model::block::INLINE_THRESHOLD;
|
||||
use garage_model::block_ref_table::*;
|
||||
use garage_model::garage::Garage;
|
||||
@@ -85,7 +86,7 @@ pub async fn handle_put(
|
||||
// Validate MD5 sum against content-md5 header and sha256sum against signed content-sha256
|
||||
if let Some(expected_sha256) = content_sha256 {
|
||||
if expected_sha256 != sha256sum {
|
||||
return Err(Error::Message(format!(
|
||||
return Err(Error::BadRequest(format!(
|
||||
"Unable to validate x-amz-content-sha256"
|
||||
)));
|
||||
} else {
|
||||
@@ -94,7 +95,7 @@ pub async fn handle_put(
|
||||
}
|
||||
if let Some(expected_md5) = content_md5 {
|
||||
if expected_md5.trim_matches('"') != md5sum {
|
||||
return Err(Error::Message(format!("Unable to validate content-md5")));
|
||||
return Err(Error::BadRequest(format!("Unable to validate content-md5")));
|
||||
} else {
|
||||
trace!("Successfully validated content-md5");
|
||||
}
|
||||
@@ -184,7 +185,7 @@ async fn put_block_meta(
|
||||
offset: u64,
|
||||
hash: Hash,
|
||||
size: u64,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<(), GarageError> {
|
||||
// TODO: don't clone, restart from empty block list ??
|
||||
let mut version = version.clone();
|
||||
version
|
||||
@@ -225,7 +226,7 @@ impl BodyChunker {
|
||||
buf: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
async fn next(&mut self) -> Result<Option<Vec<u8>>, Error> {
|
||||
async fn next(&mut self) -> Result<Option<Vec<u8>>, GarageError> {
|
||||
while !self.read_all && self.buf.len() < self.block_size {
|
||||
if let Some(block) = self.body.next().await {
|
||||
let bytes = block?;
|
||||
@@ -305,10 +306,9 @@ pub async fn handle_put_part(
|
||||
// Check parameters
|
||||
let part_number = part_number_str
|
||||
.parse::<u64>()
|
||||
.map_err(|e| Error::BadRequest(format!("Invalid part number: {}", e)))?;
|
||||
.ok_or_bad_request("Invalid part number")?;
|
||||
|
||||
let version_uuid =
|
||||
uuid_from_str(upload_id).map_err(|_| Error::BadRequest(format!("Invalid upload ID")))?;
|
||||
let version_uuid = decode_upload_id(upload_id)?;
|
||||
|
||||
let content_md5 = match req.headers().get("content-md5") {
|
||||
Some(x) => Some(x.to_str()?.to_string()),
|
||||
@@ -359,7 +359,7 @@ pub async fn handle_put_part(
|
||||
// Validate MD5 sum against content-md5 header and sha256sum against signed content-sha256
|
||||
if let Some(expected_sha256) = content_sha256 {
|
||||
if expected_sha256 != sha256sum {
|
||||
return Err(Error::Message(format!(
|
||||
return Err(Error::BadRequest(format!(
|
||||
"Unable to validate x-amz-content-sha256"
|
||||
)));
|
||||
} else {
|
||||
@@ -368,7 +368,7 @@ pub async fn handle_put_part(
|
||||
}
|
||||
if let Some(expected_md5) = content_md5 {
|
||||
if expected_md5.trim_matches('"') != md5sum {
|
||||
return Err(Error::Message(format!("Unable to validate content-md5")));
|
||||
return Err(Error::BadRequest(format!("Unable to validate content-md5")));
|
||||
} else {
|
||||
trace!("Successfully validated content-md5");
|
||||
}
|
||||
@@ -384,8 +384,7 @@ pub async fn handle_complete_multipart_upload(
|
||||
key: &str,
|
||||
upload_id: &str,
|
||||
) -> Result<Response<Body>, Error> {
|
||||
let version_uuid =
|
||||
uuid_from_str(upload_id).map_err(|_| Error::BadRequest(format!("Invalid upload ID")))?;
|
||||
let version_uuid = decode_upload_id(upload_id)?;
|
||||
|
||||
let bucket = bucket.to_string();
|
||||
let key = key.to_string();
|
||||
@@ -469,8 +468,7 @@ pub async fn handle_abort_multipart_upload(
|
||||
key: &str,
|
||||
upload_id: &str,
|
||||
) -> Result<Response<Body>, Error> {
|
||||
let version_uuid =
|
||||
uuid_from_str(upload_id).map_err(|_| Error::BadRequest(format!("Invalid upload ID")))?;
|
||||
let version_uuid = decode_upload_id(upload_id)?;
|
||||
|
||||
let object = garage
|
||||
.object_table
|
||||
@@ -532,10 +530,10 @@ fn get_headers(req: &Request<Body>) -> Result<ObjectVersionHeaders, Error> {
|
||||
})
|
||||
}
|
||||
|
||||
fn uuid_from_str(id: &str) -> Result<UUID, ()> {
|
||||
let id_bin = hex::decode(id).map_err(|_| ())?;
|
||||
fn decode_upload_id(id: &str) -> Result<UUID, Error> {
|
||||
let id_bin = hex::decode(id).ok_or_bad_request("Invalid upload ID")?;
|
||||
if id_bin.len() != 32 {
|
||||
return Err(());
|
||||
return None.ok_or_bad_request("Invalid upload ID");
|
||||
}
|
||||
let mut uuid = [0u8; 32];
|
||||
uuid.copy_from_slice(&id_bin[..]);
|
||||
|
||||
Reference in New Issue
Block a user