aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api/api_server.rs24
-rw-r--r--src/api/error.rs7
2 files changed, 16 insertions, 15 deletions
diff --git a/src/api/api_server.rs b/src/api/api_server.rs
index ec02572d..867ab953 100644
--- a/src/api/api_server.rs
+++ b/src/api/api_server.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
+use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::Arc;
@@ -50,25 +51,18 @@ async fn handler(
garage: Arc<Garage>,
req: Request<Body>,
addr: SocketAddr,
-) -> Result<Response<Body>, GarageError> {
+) -> Result<Response<Body>, Infallible> {
info!("{} {} {}", addr, req.method(), req.uri());
debug!("{:?}", req);
- match handler_inner(garage, req).await {
- Ok(x) => {
- debug!("{} {:?}", x.status(), x.headers());
- Ok(x)
- }
- Err(e) => {
- let body: Body = Body::from(format!("{}\n", e));
- let mut http_error = Response::new(body);
- *http_error.status_mut() = e.http_status_code();
- warn!("Response: error {}, {}", e.http_status_code(), e);
- Ok(http_error)
- }
- }
+ let res = controller(garage, req).await;
+ match &res {
+ Ok(x) => debug!("{} {:?}", x.status(), x.headers()),
+ Err(e) => warn!("Response: error {}, {}", e.http_status_code(), e),
+ };
+ res.or_else(|e| Ok(e.into_http_response()))
}
-async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, Error> {
+async fn controller(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, Error> {
let path = req.uri().path().to_string();
let path = percent_encoding::percent_decode_str(&path).decode_utf8()?;
diff --git a/src/api/error.rs b/src/api/error.rs
index ddb021db..a72b7f0f 100644
--- a/src/api/error.rs
+++ b/src/api/error.rs
@@ -1,5 +1,6 @@
use err_derive::Error;
use hyper::StatusCode;
+use hyper::{Body, Response};
use garage_util::error::Error as GarageError;
@@ -51,6 +52,12 @@ impl Error {
_ => StatusCode::BAD_REQUEST,
}
}
+ pub fn into_http_response(&self) -> Response<Body> {
+ let body: Body = Body::from(format!("{}\n", self));
+ let mut http_error = Response::new(body);
+ *http_error.status_mut() = self.http_status_code();
+ http_error
+ }
}
pub trait OkOrBadRequest {