Skip to content

Commit

Permalink
Improve error messages when GitHub releases fail
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanty committed Jan 21, 2025
1 parent 0be63f2 commit 625187b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
knope: patch
---

# Improve error messages when GitHub releases fail
48 changes: 36 additions & 12 deletions crates/knope/src/integrations/github/create_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use knope_config::{Asset, AssetNameError, Assets};
use miette::Diagnostic;
use relative_path::RelativePathBuf;
use tracing::info;
use ureq::Response;

use crate::{
app_config, config,
Expand Down Expand Up @@ -43,18 +44,20 @@ pub(crate) fn create_release(
);
let token_header = format!("token {}", &token);

let response: CreateReleaseResponse = agent
let response = agent
.post(&url)
.set("Authorization", &token_header)
.send_json(github_release)
.map_err(|source| Error::ApiRequest {
err: ureq_err_to_string(source),
activity: "creating a release".to_string(),
})?
.into_json()
.map_err(|source| Error::ApiResponse {
source,
activity: "creating a release",
})
.and_then(|resp| error_on_bad_http_status(resp, "creating a release".to_string()))?;

let response: CreateReleaseResponse =
response.into_json().map_err(|source| Error::ApiResponse {
message: source.to_string(),
activity: "creating a release".to_string(),
})?;

if let Some(assets) = assets {
Expand All @@ -79,6 +82,14 @@ pub(crate) fn create_release(
activity: format!(
"uploading asset {asset_name}. Release has been created but not published!",
),
})
.and_then(|resp| {
error_on_bad_http_status(
resp,
format!(
"uploading asset {asset_name}. Release has been created but not published!",
),
)
})?;
}
agent
Expand All @@ -90,12 +101,28 @@ pub(crate) fn create_release(
.map_err(|source| Error::ApiRequest {
err: ureq_err_to_string(source),
activity: "publishing release".to_string(),
})?;
})
.and_then(|resp| error_on_bad_http_status(resp, "publishing release".to_string()))?;
}

Ok(state::GitHub::Initialized { token, agent })
}

fn error_on_bad_http_status(response: Response, activity: String) -> Result<Response, Error> {
if response.status() >= 400 {
let num = response.status();
let text = response.status_text().to_string();
let message = if let Ok(body) = response.into_string() {
format!("Got HTTP status {num} {text} with body {body}")
} else {
format!("Got HTTP status {num} {text}")
};
Err(Error::ApiResponse { message, activity })
} else {
Ok(response)
}
}

fn github_release_dry_run(
name: &str,
assets: Option<&Assets>,
Expand Down Expand Up @@ -164,17 +191,14 @@ pub(crate) enum Error {
)
)]
ApiRequest { err: String, activity: String },
#[error("Trouble decoding the response from GitHub while {activity}: {source}")]
#[error("Trouble decoding the response from GitHub while {activity}: {message}")]
#[diagnostic(
code(github::api_response_error),
help(
"Failure to decode a response from GitHub is probably a bug. Please report it at https://github.com/knope-dev/knope"
)
)]
ApiResponse {
source: std::io::Error,
activity: &'static str,
},
ApiResponse { message: String, activity: String },
#[error("Asset was not uploaded to GitHub, a release was created but is still a draft! {0}")]
#[diagnostic(
code(github::asset_name_error),
Expand Down

0 comments on commit 625187b

Please sign in to comment.