Skip to content

Commit 27d4057

Browse files
committed
log
1 parent c0e348d commit 27d4057

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

crates/catalog/rest/src/client.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
1817
use std::collections::HashMap;
1918
use std::fmt::{Debug, Formatter};
2019
use std::sync::Mutex;
2120

2221
use iceberg::{Error, ErrorKind, Result};
22+
use log::debug;
2323
use reqwest::header::HeaderMap;
2424
use reqwest::{Client, IntoUrl, Method, Request, RequestBuilder, Response};
2525
use serde::de::DeserializeOwned;
@@ -236,36 +236,44 @@ impl HttpClient {
236236

237237
let resp = self.client.execute(request).await?;
238238

239-
if resp.status().as_u16() == SUCCESS_CODE {
240-
let text = resp
241-
.bytes()
242-
.await
243-
.map_err(|err| err.with_url(url.clone()))?;
244-
Ok(serde_json::from_slice::<R>(&text).map_err(|e| {
239+
// Capture the status code before consuming the response body.
240+
let status = resp.status();
241+
242+
// Consume the response body once and store the bytes.
243+
let bytes = resp
244+
.bytes()
245+
.await
246+
.map_err(|err| err.with_url(url.clone()))?;
247+
248+
// Optionally, log the response.
249+
debug!(
250+
"Response from {} {}: {}",
251+
method,
252+
url,
253+
String::from_utf8_lossy(&bytes)
254+
);
255+
256+
if status.as_u16() == SUCCESS_CODE {
257+
Ok(serde_json::from_slice::<R>(&bytes).map_err(|e| {
245258
Error::new(
246259
ErrorKind::Unexpected,
247260
"Failed to parse response from rest catalog server!",
248261
)
249262
.with_context("method", method.to_string())
250263
.with_context("url", url.to_string())
251-
.with_context("json", String::from_utf8_lossy(&text))
264+
.with_context("json", String::from_utf8_lossy(&bytes))
252265
.with_source(e)
253266
})?)
254267
} else {
255-
let code = resp.status();
256-
let text = resp
257-
.bytes()
258-
.await
259-
.map_err(|err| err.with_url(url.clone()))?;
260-
let e = serde_json::from_slice::<E>(&text).map_err(|e| {
268+
let e = serde_json::from_slice::<E>(&bytes).map_err(|e| {
261269
Error::new(
262270
ErrorKind::Unexpected,
263271
"Failed to parse response from rest catalog server!",
264272
)
265-
.with_context("code", code.to_string())
273+
.with_context("code", status.to_string())
266274
.with_context("method", method.to_string())
267275
.with_context("url", url.to_string())
268-
.with_context("json", String::from_utf8_lossy(&text))
276+
.with_context("json", String::from_utf8_lossy(&bytes))
269277
.with_source(e)
270278
})?;
271279
Err(e.into())

0 commit comments

Comments
 (0)