Skip to content

Commit

Permalink
change sign_url error response
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshidan committed Jan 30, 2025
1 parent 55e6afa commit 89ecac1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions storage/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub enum Error {
/// Invalid Range error
#[error("invalid range header, received: {0}")]
InvalidRangeHeader(String),

#[error("Request failed: {0} detail={1}")]
RawResponse(reqwest::Error, String),
}

impl From<reqwest_middleware::Error> for Error {
Expand Down
20 changes: 16 additions & 4 deletions storage/src/http/service_account_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::Arc;

use google_cloud_token::TokenSource;
use reqwest::Response;
use std::sync::Arc;

use crate::http::{check_response_status, Error};
use crate::http::Error;

#[derive(Clone)]
pub struct ServiceAccountClient {
Expand Down Expand Up @@ -42,9 +42,21 @@ impl ServiceAccountClient {
None => request,
};
let response = request.send().await?;
let response = check_response_status(response).await?;
let response = ServiceAccountClient::check_response_status(response).await?;
Ok(response.json::<SignBlobResponse>().await?.signed_blob)
}

/// Checks whether an HTTP response is successful and returns it, or returns an error.
async fn check_response_status(response: Response) -> Result<Response, Error> {
// Check the status code, returning the response if it is not an error.
match response.error_for_status_ref() {
Ok(_) => Ok(response),
Err(error) => match response.text().await {
Ok(raw) => Err(Error::RawResponse(error, raw)),
Err(_) => Err(Error::HttpClient(error)),
},
}
}
}

#[derive(serde::Serialize)]
Expand Down

0 comments on commit 89ecac1

Please sign in to comment.