From b947b1e7ee93bdc1a842bccd11d5a3914a26611f Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Thu, 15 Oct 2020 09:03:54 +0200 Subject: [PATCH] server: rest: refactor code to avoid multiple log_response calls The 'Ok::<_, Self::Error>(res)' type annotation was from a time where we could not use async, and had a combinator here which needed explicity type information. We switched over to async in commit 91e4587343c155cd3aa9274bd2c736dcc1ccf977 and, as the type annotation is already included in the Future type, we can safely drop it. Signed-off-by: Thomas Lamprecht --- src/server/rest.rs | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/server/rest.rs b/src/server/rest.rs index 6b20c57f..d145573f 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -143,25 +143,18 @@ impl tower_service::Service> for ApiService { let config = Arc::clone(&self.api_config); let peer = self.peer; async move { - match handle_request(config, req).await { - Ok(res) => { - log_response(&peer, method, &path, &res); - Ok::<_, Self::Error>(res) - } + let response = match handle_request(config, req).await { + Ok(response) => response, Err(err) => { - if let Some(apierr) = err.downcast_ref::() { - let mut resp = Response::new(Body::from(apierr.message.clone())); - *resp.status_mut() = apierr.code; - log_response(&peer, method, &path, &resp); - Ok(resp) - } else { - let mut resp = Response::new(Body::from(err.to_string())); - *resp.status_mut() = StatusCode::BAD_REQUEST; - log_response(&peer, method, &path, &resp); - Ok(resp) - } + let (err, code) = match err.downcast_ref::() { + Some(apierr) => (apierr.message.clone(), apierr.code), + _ => (err.to_string(), StatusCode::BAD_REQUEST), + }; + Response::builder().status(code).body(err.into())? } - } + }; + log_response(&peer, method, &path, &response); + Ok(response) } .boxed() }