public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 2/3] s3 client: refactor error response body into dedicated helper
Date: Wed, 23 Jul 2025 12:26:00 +0200	[thread overview]
Message-ID: <20250723102601.495806-3-c.ebner@proxmox.com> (raw)
In-Reply-To: <20250723102601.495806-1-c.ebner@proxmox.com>

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@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



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2025-07-23 10:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-23 10:25 [pbs-devel] [PATCH proxmox 0/3] s3 client: improve error logging for invalid requests Christian Ebner
2025-07-23 10:25 ` [pbs-devel] [PATCH proxmox 1/3] s3 client: fix minor whitespace issue Christian Ebner
2025-07-23 10:26 ` Christian Ebner [this message]
2025-07-23 10:26 ` [pbs-devel] [PATCH proxmox 3/3] s3 client: log error response body for invalid request status codes Christian Ebner
2025-07-23 11:31 ` [pbs-devel] [PATCH proxmox 0/3] s3 client: improve error logging for invalid requests Lukas Wagner
2025-07-23 11:51 ` [pbs-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250723102601.495806-3-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal