Skip to content

Commit fd6054e

Browse files
nipunn1313Convex, Inc.
authored andcommitted
Handle export-in-progress (#30194)
GitOrigin-RevId: 4b90225e3920eb726051cff683fa08520c4ba27a
1 parent c7d3714 commit fd6054e

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

crates/common/src/http/mod.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use http::{
6868
StatusCode,
6969
Uri,
7070
};
71+
use http_body_util::BodyExt;
7172
use itertools::Itertools;
7273
use minitrace::{
7374
future::FutureExt as _,
@@ -478,40 +479,34 @@ impl HttpError {
478479
}
479480
}
480481

481-
#[cfg(any(test, feature = "testing"))]
482482
pub async fn error_message_from_bytes(
483-
bytes: hyper::body::Bytes,
484-
) -> (Cow<'static, str>, Cow<'static, str>) {
483+
bytes: &hyper::body::Bytes,
484+
) -> anyhow::Result<(Cow<'static, str>, Cow<'static, str>)> {
485485
let ResponseErrorMessage { code, message } =
486-
serde_json::from_slice(&bytes).unwrap_or_else(|_| {
487-
panic!(
488-
"Couldn't deserialize as json: {}",
489-
String::from_utf8_lossy(&bytes)
490-
)
491-
});
492-
493-
(code, message)
494-
}
486+
serde_json::from_slice(bytes).context(format!(
487+
"Couldn't deserialize as json: {}",
488+
String::from_utf8_lossy(bytes)
489+
))?;
495490

496-
// Tests might parse a response back into a message
497-
#[cfg(any(test, feature = "testing"))]
498-
pub async fn from_response(response: Response) -> Self {
499-
use http_body_util::BodyExt;
491+
Ok((code, message))
492+
}
500493

494+
pub async fn from_response(response: Response) -> anyhow::Result<Self> {
501495
let (parts, body) = response.into_parts();
502496
let (code, message) = Self::error_message_from_bytes(
503-
body.collect()
497+
&body
498+
.collect()
504499
.await
505500
.expect("Couldn't collect body")
506501
.to_bytes(),
507502
)
508-
.await;
503+
.await?;
509504

510-
Self {
505+
Ok(Self {
511506
status_code: parts.status,
512507
error_code: code,
513508
msg: message,
514-
}
509+
})
515510
}
516511
}
517512

@@ -1202,7 +1197,7 @@ mod tests {
12021197
use crate::http::HttpError;
12031198

12041199
#[tokio::test]
1205-
async fn test_http_response_error_internal_server_error() {
1200+
async fn test_http_response_error_internal_server_error() -> anyhow::Result<()> {
12061201
let err_text = "some random error";
12071202
let err = anyhow::anyhow!(err_text);
12081203
let err_clone = anyhow::anyhow!(err_text);
@@ -1222,14 +1217,15 @@ mod tests {
12221217
// Check the Response contains the ResponseErrorMessage
12231218
let http_response_err: HttpResponseError = err_clone.into();
12241219
let response = http_response_err.into_response();
1225-
let error = HttpError::from_response(response).await;
1220+
let error = HttpError::from_response(response).await?;
12261221
assert_eq!(error.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
12271222
assert_eq!(error.error_code(), "InternalServerError");
12281223
assert_eq!(error.msg, INTERNAL_SERVER_ERROR_MSG);
1224+
Ok(())
12291225
}
12301226

12311227
#[tokio::test]
1232-
async fn test_http_error_400() {
1228+
async fn test_http_error_400() -> anyhow::Result<()> {
12331229
let status_code = StatusCode::BAD_REQUEST;
12341230
let error_code = "ErrorCode";
12351231
let msg = "Nice error message!";
@@ -1258,9 +1254,10 @@ mod tests {
12581254
// Check the Response contains the ResponseErrorMessage
12591255
let http_response_err: HttpResponseError = err_clone.into();
12601256
let response = http_response_err.into_response();
1261-
let error = HttpError::from_response(response).await;
1257+
let error = HttpError::from_response(response).await?;
12621258
assert_eq!(error.status_code(), status_code);
12631259
assert_eq!(error.error_code(), error_code);
12641260
assert_eq!(error.message(), msg);
1261+
Ok(())
12651262
}
12661263
}

crates/local_backend/src/test_helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl TestLocalBackend {
100100
) -> anyhow::Result<()> {
101101
tracing::info!("Sending req {req:?}");
102102
let response = self.app.router().clone().oneshot(req).await?;
103-
let error = HttpError::from_response(response).await;
103+
let error = HttpError::from_response(response).await?;
104104
tracing::info!("Got {error:?}");
105105
assert_eq!(error.status_code(), expected_code);
106106
assert_eq!(error.error_code(), expected_short_msg);

0 commit comments

Comments
 (0)