From: Thomas Lamprecht Date: Thu, 15 Oct 2020 07:03:54 +0000 (+0200) Subject: server: rest: refactor code to avoid multiple log_response calls X-Git-Tag: v0.9.2~114 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=b947b1e7ee93bdc1a842bccd11d5a3914a26611f;p=proxmox-backup.git 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 --- 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() }