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/api_server.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/api_server.rs')
-rw-r--r-- | src/api/api_server.rs | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/src/api/api_server.rs b/src/api/api_server.rs index 9dc74dac..ec02572d 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -7,10 +7,11 @@ use hyper::server::conn::AddrStream; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Method, Request, Response, Server}; -use garage_util::error::Error; +use garage_util::error::Error as GarageError; use garage_model::garage::Garage; +use crate::error::*; use crate::signature::check_signature; use crate::s3_copy::*; @@ -22,14 +23,14 @@ use crate::s3_put::*; pub async fn run_api_server( garage: Arc<Garage>, shutdown_signal: impl Future<Output = ()>, -) -> Result<(), Error> { +) -> Result<(), GarageError> { let addr = &garage.config.s3_api.api_bind_addr; let service = make_service_fn(|conn: &AddrStream| { let garage = garage.clone(); let client_addr = conn.remote_addr(); async move { - Ok::<_, Error>(service_fn(move |req: Request<Body>| { + Ok::<_, GarageError>(service_fn(move |req: Request<Body>| { let garage = garage.clone(); handler(garage, req, client_addr) })) @@ -49,7 +50,7 @@ async fn handler( garage: Arc<Garage>, req: Request<Body>, addr: SocketAddr, -) -> Result<Response<Body>, Error> { +) -> Result<Response<Body>, GarageError> { info!("{} {} {}", addr, req.method(), req.uri()); debug!("{:?}", req); match handler_inner(garage, req).await { @@ -131,10 +132,7 @@ async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Respon source_bucket ))); } - let source_key = match source_key { - None => return Err(Error::BadRequest(format!("No source key specified"))), - Some(x) => x, - }; + let source_key = source_key.ok_or_bad_request("No source key specified")?; Ok(handle_copy(garage, &bucket, &key, &source_bucket, &source_key).await?) } else { // PutObject query @@ -205,9 +203,8 @@ async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Respon let max_keys = params .get("max-keys") .map(|x| { - x.parse::<usize>().map_err(|e| { - Error::BadRequest(format!("Invalid value for max-keys: {}", e)) - }) + x.parse::<usize>() + .ok_or_bad_request("Invalid value for max-keys") }) .unwrap_or(Ok(1000))?; let prefix = params.get("prefix").map(|x| x.as_str()).unwrap_or(&""); |