[pbs-devel] [PATCH proxmox 2/3] s3 client: refactor error response body into dedicated helper

Christian Ebner c.ebner at proxmox.com
Wed Jul 23 12:26:00 CEST 2025


Places body utf8 encoding and logging into a common place instead
of having it scattered over various places in the response status
checks.

No functional changes intended.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 proxmox-s3-client/src/response_reader.rs | 46 ++++++++----------------
 1 file changed, 15 insertions(+), 31 deletions(-)

diff --git a/proxmox-s3-client/src/response_reader.rs b/proxmox-s3-client/src/response_reader.rs
index 41e8c6d8..7aed4201 100644
--- a/proxmox-s3-client/src/response_reader.rs
+++ b/proxmox-s3-client/src/response_reader.rs
@@ -2,7 +2,7 @@ use std::str::FromStr;
 
 use anyhow::{anyhow, bail, Context, Error};
 use http_body_util::BodyExt;
-use hyper::body::Incoming;
+use hyper::body::{Bytes, Incoming};
 use hyper::header::HeaderName;
 use hyper::http::header;
 use hyper::http::StatusCode;
@@ -166,11 +166,7 @@ impl ResponseReader {
             StatusCode::OK => (),
             StatusCode::NOT_FOUND => bail!("bucket does not exist"),
             status_code => {
-                if let Ok(body) = String::from_utf8(body.to_vec()) {
-                    if !body.is_empty() {
-                        tracing::error!("{body}");
-                    }
-                }
+                Self::log_error_response_utf8(body);
                 bail!("unexpected status code {status_code}")
             }
         }
@@ -193,11 +189,7 @@ impl ResponseReader {
             StatusCode::OK => (),
             StatusCode::NOT_FOUND => return Ok(None),
             status_code => {
-                if let Ok(body) = String::from_utf8(body.to_vec()) {
-                    if !body.is_empty() {
-                        tracing::error!("{body}");
-                    }
-                }
+                Self::log_error_response_utf8(body);
                 bail!("unexpected status code {status_code}")
             }
         }
@@ -229,11 +221,7 @@ impl ResponseReader {
             StatusCode::FORBIDDEN => bail!("object is archived and inaccessible until restored"),
             status_code => {
                 let body = content.collect().await?.to_bytes();
-                if let Ok(body) = String::from_utf8(body.to_vec()) {
-                    if !body.is_empty() {
-                        tracing::error!("{body}");
-                    }
-                }
+                Self::log_error_response_utf8(body);
                 bail!("unexpected status code {status_code}")
             }
         }
@@ -264,11 +252,7 @@ impl ResponseReader {
             StatusCode::CONFLICT => return Ok(PutObjectResponse::NeedsRetry),
             StatusCode::BAD_REQUEST => bail!("invalid request"),
             status_code => {
-                if let Ok(body) = String::from_utf8(body.to_vec()) {
-                    if !body.is_empty() {
-                        tracing::error!("{body}");
-                    }
-                }
+                Self::log_error_response_utf8(body);
                 bail!("unexpected status code {status_code}")
             }
         };
@@ -301,11 +285,7 @@ impl ResponseReader {
             StatusCode::OK => (),
             StatusCode::BAD_REQUEST => bail!("invalid request"),
             status_code => {
-                if let Ok(body) = String::from_utf8(body.to_vec()) {
-                    if !body.is_empty() {
-                        tracing::error!("{body}");
-                    }
-                }
+                Self::log_error_response_utf8(body);
                 bail!("unexpected status code {status_code}")
             }
         };
@@ -327,11 +307,7 @@ impl ResponseReader {
             StatusCode::NOT_FOUND => bail!("object not found"),
             StatusCode::FORBIDDEN => bail!("the source object is not in the active tier"),
             status_code => {
-                if let Ok(body) = String::from_utf8(body.to_vec()) {
-                    if !body.is_empty() {
-                        tracing::error!("{body}");
-                    }
-                }
+                Self::log_error_response_utf8(body);
                 bail!("unexpected status code {status_code}")
             }
         }
@@ -357,6 +333,14 @@ impl ResponseReader {
         })
     }
 
+    fn log_error_response_utf8(body: Bytes) {
+        if let Ok(body) = String::from_utf8(body.to_vec()) {
+            if !body.is_empty() {
+                tracing::error!("{body}");
+            }
+        }
+    }
+
     fn parse_header<T: FromStr>(name: HeaderName, headers: &HeaderMap) -> Result<T, Error>
     where
         <T as FromStr>::Err: Send + Sync + 'static,
-- 
2.47.2





More information about the pbs-devel mailing list