diff options
author | Alex <alex@adnab.me> | 2020-11-08 18:04:52 +0100 |
---|---|---|
committer | Alex <alex@adnab.me> | 2020-11-08 18:04:52 +0100 |
commit | 045009da9b7ac4198574bd5aa256c11cfe4ae469 (patch) | |
tree | d3014cc2739c5a1bce62ff35efc8417b79ff46ef /src/api/s3_put.rs | |
parent | 54166d2a09f488bff080469160d4df6a78db1a3f (diff) | |
parent | a50fa70d45f8b5af68d23d60c3bac2af4ecceb58 (diff) | |
download | garage-045009da9b7ac4198574bd5aa256c11cfe4ae469.tar.gz garage-045009da9b7ac4198574bd5aa256c11cfe4ae469.zip |
Merge pull request 'Refactor error management in API part' (#10) from error-refactoring into master
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/10
Diffstat (limited to 'src/api/s3_put.rs')
-rw-r--r-- | src/api/s3_put.rs | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/src/api/s3_put.rs b/src/api/s3_put.rs index 0926ba89..ea09524c 100644 --- a/src/api/s3_put.rs +++ b/src/api/s3_put.rs @@ -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[..]); |