]> git.proxmox.com Git - proxmox-backup.git/commitdiff
server: rest: refactor code to avoid multiple log_response calls
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 15 Oct 2020 07:03:54 +0000 (09:03 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 15 Oct 2020 11:58:47 +0000 (13:58 +0200)
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 <t.lamprecht@proxmox.com>
src/server/rest.rs

index 6b20c57fb90854961366dd7f500fb8ed26901740..d145573ff8fee1c91a3ba65b1334ca2eb2897a89 100644 (file)
@@ -143,25 +143,18 @@ impl tower_service::Service<Request<Body>> 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::<HttpError>() {
-                        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::<HttpError>() {
+                        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()
     }