Skip to content

Commit

Permalink
feat(module-index-client): Catch 404 errors on get requests in module…
Browse files Browse the repository at this point in the history
… index client
  • Loading branch information
stack72 committed Feb 26, 2025
1 parent 3ca9c38 commit ce3a73c
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions lib/module-index-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum ModuleIndexClientError {
Deserialization(serde_json::Error),
#[error("Request error: {0}")]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
#[error("module not found (module id: {0})")]
ModuleNotFound(String),
#[error("Request error: {0}")]
Request(#[from] reqwest::Error),
#[error("Serialization error: {0}")]
Expand Down Expand Up @@ -63,8 +65,15 @@ impl ModuleIndexClient {
)
.bearer_auth(&self.auth_token)
.send()
.await?
.error_for_status()?;
.await?;

if upload_response.status() == reqwest::StatusCode::NOT_FOUND {
return Err(ModuleIndexClientError::ModuleNotFound(
module_id.to_string(),
));
}

let upload_response = upload_response.error_for_status()?;

Ok(upload_response.json::<ModuleRejectionResponse>().await?)
}
Expand All @@ -88,8 +97,15 @@ impl ModuleIndexClient {
)
.bearer_auth(&self.auth_token)
.send()
.await?
.error_for_status()?;
.await?;

if promote_response.status() == reqwest::StatusCode::NOT_FOUND {
return Err(ModuleIndexClientError::ModuleNotFound(
module_id.to_string(),
));
}

let promote_response = promote_response.error_for_status()?;

Ok(promote_response.json::<ModulePromotedResponse>().await?)
}
Expand Down Expand Up @@ -218,9 +234,15 @@ impl ModuleIndexClient {
.get(download_url)
.bearer_auth(&self.auth_token)
.send()
.await?
.error_for_status()?;
.await?;

if response.status() == reqwest::StatusCode::NOT_FOUND {
return Err(ModuleIndexClientError::ModuleNotFound(
module_id.to_string(),
));
}

let response = response.error_for_status()?;
let bytes = response.bytes().await?;

Ok(bytes.to_vec())
Expand Down Expand Up @@ -264,15 +286,23 @@ impl ModuleIndexClient {
.base_url
.join("modules/")?
.join(&format!("{}", module_id))?;

Ok(reqwest::Client::new()
let response = reqwest::Client::new()
.get(details_url)
.bearer_auth(&self.auth_token)
.send()
.await?
.error_for_status()?
.json()
.await?)
.await?;

if response.status() == reqwest::StatusCode::NOT_FOUND {
return Err(ModuleIndexClientError::ModuleNotFound(
module_id.to_string(),
));
}

let response = response.error_for_status()?;
let response = response.json().await?;

Ok(response)
}

pub async fn get_builtin(&self, module_id: Ulid) -> ModuleIndexClientResult<Vec<u8>> {
Expand Down

0 comments on commit ce3a73c

Please sign in to comment.