Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/client/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,11 @@ pub fn format_fail_message(
util::api_error(body.to_string())
);
}
// An empty body carries no explanation of its own — the status line is
// the only signal there is, so surface it instead of a blank message.
if body.trim().is_empty() {
return format!("error: HTTP {status} (empty response body)");
}
util::api_error(body.to_string())
}

Expand Down Expand Up @@ -973,6 +978,18 @@ mod tests {
assert_eq!(msg, "server exploded");
}

#[test]
fn format_fail_message_empty_body_surfaces_status() {
// An empty response body used to fall through to a blank message
// (an error line that was only ANSI color codes). The HTTP status is
// the only signal available, so it must appear.
let msg = format_fail_message(reqwest::StatusCode::BAD_GATEWAY, "", None);
assert_eq!(msg, "error: HTTP 502 Bad Gateway (empty response body)");

let msg = format_fail_message(reqwest::StatusCode::SERVICE_UNAVAILABLE, " \n", None);
assert!(msg.contains("503"), "got: {msg}");
}

#[test]
fn format_fail_message_4xx_connection_error_on_probe_falls_through() {
let body = r#"{"error":{"message":"forbidden"}}"#;
Expand Down
19 changes: 19 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ pub fn api_error(body: String) -> String {
if body.trim_start().starts_with('<') {
return "unexpected server error".to_string();
}
// A dropped connection or bodyless 5xx yields an empty body; returning it
// verbatim would print as a blank (color-codes-only) error line.
if body.trim().is_empty() {
return "unexpected empty response from server".to_string();
}
body
}

Expand Down Expand Up @@ -459,6 +464,20 @@ mod tests {
assert_eq!(api_error(body), "unexpected server error");
}

#[test]
fn api_error_never_returns_blank_for_empty_body() {
// A dropped connection or bodyless 5xx used to echo the empty body,
// printing an error line that was nothing but color codes.
assert_eq!(
api_error(String::new()),
"unexpected empty response from server"
);
assert_eq!(
api_error(" \n".to_string()),
"unexpected empty response from server"
);
}

#[test]
fn is_access_denied_detects_code() {
assert!(is_access_denied(
Expand Down
Loading