Skip to content

Commit b42d61c

Browse files
committed
Minor cleanup based on feedback
1 parent 0c1a35f commit b42d61c

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

crates/common/src/config/utils.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,25 @@ pub fn load_jwt_secrets() -> Result<HashMap<ModuleId, String>> {
3636
pub async fn safe_read_http_response(response: reqwest::Response) -> Result<Vec<u8>> {
3737
// Read the response to a buffer in chunks
3838
let status_code = response.status();
39-
let response_bytes = read_chunked_body_with_max(response, MUXER_HTTP_MAX_LENGTH)
40-
.await?;
41-
42-
// Make sure the response is a 200
43-
if status_code != reqwest::StatusCode::OK {
44-
match response_bytes {
45-
Ok(bytes) => {
46-
bail!(
47-
"Request failed with status: {}, body: {}",
48-
status_code,
49-
String::from_utf8_lossy(&bytes)
50-
);
39+
match read_chunked_body_with_max(response, MUXER_HTTP_MAX_LENGTH).await {
40+
Ok(response_bytes) => {
41+
if status_code.is_success() {
42+
return Ok(response_bytes);
5143
}
52-
Err(e) => {
53-
bail!(
54-
"Request failed with status: {} but decoding the response body failed: {}",
55-
status_code,
56-
e
57-
);
44+
bail!(
45+
"Request failed with status: {status_code}, body: {}",
46+
String::from_utf8_lossy(&response_bytes)
47+
)
48+
}
49+
Err(e) => {
50+
if status_code.is_success() {
51+
return Err(e).wrap_err("Failed to read response body");
5852
}
53+
Err(e).wrap_err(format!(
54+
"Request failed with status {status_code}, but decoding the response body failed"
55+
))
5956
}
6057
}
61-
response_bytes
6258
}
6359

6460
/// Removes duplicate entries from a vector of BlsPublicKey

crates/common/src/utils.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#[cfg(test)]
2+
use std::cell::Cell;
13
use std::{
2-
cell::Cell,
34
net::Ipv4Addr,
45
time::{SystemTime, UNIX_EPOCH},
56
};
@@ -45,14 +46,17 @@ pub enum ResponseReadError {
4546
ReqwestError(#[from] reqwest::Error),
4647
}
4748

49+
#[cfg(test)]
4850
thread_local! {
4951
static IGNORE_CONTENT_LENGTH: Cell<bool> = const { Cell::new(false) };
5052
}
5153

54+
#[cfg(test)]
5255
pub fn set_ignore_content_length(val: bool) {
5356
IGNORE_CONTENT_LENGTH.with(|f| f.set(val));
5457
}
5558

59+
#[cfg(test)]
5660
fn should_ignore_content_length() -> bool {
5761
IGNORE_CONTENT_LENGTH.with(|f| f.get())
5862
}
@@ -64,7 +68,13 @@ pub async fn read_chunked_body_with_max(
6468
max_size: usize,
6569
) -> Result<Vec<u8>, ResponseReadError> {
6670
// Get the content length from the response headers
71+
#[cfg(not(test))]
72+
let content_length = res.content_length();
73+
74+
#[cfg(test)]
6775
let mut content_length = res.content_length();
76+
77+
#[cfg(test)]
6878
if should_ignore_content_length() {
6979
// Used for testing purposes to ignore content length
7080
content_length = None;

0 commit comments

Comments
 (0)