Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check err on Client::request_stream #1433

Merged
merged 1 commit into from
Mar 21, 2024
Merged
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
32 changes: 18 additions & 14 deletions kube-client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! retrieve the resources served by the kubernetes API.
use either::{Either, Left, Right};
use futures::{self, AsyncBufRead, StreamExt, TryStream, TryStreamExt};
use http::{self, Request, Response, StatusCode};

Check warning on line 12 in kube-client/src/client/mod.rs

View workflow job for this annotation

GitHub Actions / msrv

unused import: `StatusCode`

Check warning on line 12 in kube-client/src/client/mod.rs

View workflow job for this annotation

GitHub Actions / msrv

unused import: `StatusCode`
use hyper::Body;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as k8s_meta_v1;
pub use kube_core::response::Status;
Expand Down Expand Up @@ -226,14 +226,11 @@
/// as a string
pub async fn request_text(&self, request: Request<Vec<u8>>) -> Result<String> {
let res = self.send(request.map(Body::from)).await?;
let status = res.status();
// trace!("Status = {:?} for {}", status, res.url());
let res = handle_api_errors(res).await?;
let body_bytes = hyper::body::to_bytes(res.into_body())
.await
.map_err(Error::HyperError)?;
let text = String::from_utf8(body_bytes.to_vec()).map_err(Error::FromUtf8)?;
handle_api_errors(&text, status)?;

Ok(text)
}

Expand All @@ -243,6 +240,7 @@
/// and [`AsyncBufReadExt`](futures::AsyncBufReadExt).
pub async fn request_stream(&self, request: Request<Vec<u8>>) -> Result<impl AsyncBufRead> {
let res = self.send(request.map(Body::from)).await?;
let res = handle_api_errors(res).await?;
// Map the error, since we want to convert this into an `AsyncBufReader` using
// `into_async_read` which specifies `std::io::Error` as the stream's error type.
let body = res
Expand Down Expand Up @@ -434,26 +432,32 @@
///
/// In either case, present an ApiError upstream.
/// The latter is probably a bug if encountered.
fn handle_api_errors(text: &str, s: StatusCode) -> Result<()> {
if s.is_client_error() || s.is_server_error() {
async fn handle_api_errors(res: Response<Body>) -> Result<Response<Body>> {
let status = res.status();
if status.is_client_error() || status.is_server_error() {
// trace!("Status = {:?} for {}", status, res.url());
let body_bytes = hyper::body::to_bytes(res.into_body())
.await
.map_err(Error::HyperError)?;

Check warning on line 441 in kube-client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/mod.rs#L441

Added line #L441 was not covered by tests
let text = String::from_utf8(body_bytes.to_vec()).map_err(Error::FromUtf8)?;
// Print better debug when things do fail
// trace!("Parsing error: {}", text);
if let Ok(errdata) = serde_json::from_str::<ErrorResponse>(text) {
tracing::debug!("Unsuccessful: {:?}", errdata);
if let Ok(errdata) = serde_json::from_str::<ErrorResponse>(&text) {
tracing::debug!("Unsuccessful: {errdata:?}");
Err(Error::Api(errdata))
} else {
tracing::warn!("Unsuccessful data error parse: {}", text);
let ae = ErrorResponse {
status: s.to_string(),
code: s.as_u16(),
let error_response = ErrorResponse {
status: status.to_string(),
code: status.as_u16(),
message: format!("{text:?}"),
reason: "Failed to parse error data".into(),
};
tracing::debug!("Unsuccessful: {:?} (reconstruct)", ae);
Err(Error::Api(ae))
tracing::debug!("Unsuccessful: {error_response:?} (reconstruct)");
Err(Error::Api(error_response))
}
} else {
Ok(())
Ok(res)

Check warning on line 460 in kube-client/src/client/mod.rs

View check run for this annotation

Codecov / codecov/patch

kube-client/src/client/mod.rs#L460

Added line #L460 was not covered by tests
}
}

Expand Down
Loading