Skip to content

Commit

Permalink
Apply text response handling to other status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Jun 26, 2024
1 parent 6fc7bfd commit 71a4299
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,10 @@ async fn execute() -> Result<std::process::ExitCode> {
}
}
StatusCode::UNAUTHORIZED => {
let body = response.bytes().await?;
let message = serde_json::from_slice::<String>(&body)?;

return Err(Error::Unauthorized(message))?;
return Err(Error::Unauthorized(response_text(response).await))?;
}
StatusCode::BAD_REQUEST => {
return Err(Error::BadRequest(handle_message(response).await))?;
return Err(Error::BadRequest(response_text(response).await))?;
}
_ => {
return Err(eyre!(
Expand All @@ -131,7 +128,7 @@ async fn execute() -> Result<std::process::ExitCode> {
{}\
",
response_status,
handle_message(response).await,
response_text(response).await,
));
}
}
Expand All @@ -153,7 +150,7 @@ async fn execute() -> Result<std::process::ExitCode> {
Ok(ExitCode::SUCCESS)
}

async fn handle_message(res: Response) -> String {
async fn response_text(res: Response) -> String {
if let Ok(message) = res.text().await {
message
} else {
Expand Down

0 comments on commit 71a4299

Please sign in to comment.