Skip to content

Commit

Permalink
Merge pull request #105 from DeterminateSystems/hoverbear/fh-196-bloc…
Browse files Browse the repository at this point in the history
…k-new-user-and-organization-creation-on-flakehub-push

Offer an improved error experience
  • Loading branch information
cole-h authored Feb 9, 2024
2 parents c51423e + 4255ac4 commit e38b538
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
41 changes: 21 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ github-actions-oidc-claims = "0.3.0"
spdx = "0.10.2"
uuid = { version = "1.4.0", features = ["serde", "v7", "rand", "std"] }
semver = { version = "1.0.18", features = ["serde"] }
thiserror = "1.0.56"

[profile.release]
strip = true # Automatically strip symbols from the binary.
Expand Down
2 changes: 1 addition & 1 deletion src/cli/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<'a> Instrumentation {
_ => return Err(e).wrap_err_with(|| "parsing RUST_LOG directives"),
}
}
EnvFilter::try_new(&format!(
EnvFilter::try_new(format!(
"{}={}",
env!("CARGO_PKG_NAME").replace('-', "_"),
self.log_level()
Expand Down
25 changes: 18 additions & 7 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tokio::io::AsyncWriteExt;
use uuid::Uuid;

use crate::{
error::Error,
flake_info::{check_flake_evaluates, get_flake_metadata, get_flake_outputs, get_flake_tarball},
graphql::{GithubGraphqlDataQuery, GithubGraphqlDataResult},
release_metadata::{ReleaseMetadata, RevisionInfo},
Expand Down Expand Up @@ -714,26 +715,36 @@ async fn push_new_release(
"Got release metadata POST response"
);

if release_metadata_post_response_status != StatusCode::OK {
if release_metadata_post_response_status == StatusCode::CONFLICT {
match release_metadata_post_response_status {
StatusCode::OK => (),
StatusCode::CONFLICT => {
tracing::info!(
"Release for revision `{revision}` of {upload_name}/{rolling_prefix_or_tag} already exists; flakehub-push will not upload it again",
revision = release_metadata.revision
);
if error_if_release_conflicts {
return Err(color_eyre::eyre::eyre!(
"{upload_name}/{rolling_prefix_or_tag} already exists"
));
return Err(Error::Conflict {
upload_name,
rolling_prefix_or_tag,
})?;
} else {
return Ok(());
}
} else {
}
StatusCode::UNAUTHORIZED => {
let body = &release_metadata_post_response.bytes().await?;
let message = serde_json::from_slice::<String>(body)?;
return Err(Error::Unauthorized(message))?;
}
_ => {
let body = &release_metadata_post_response.bytes().await?;
let message = serde_json::from_slice::<String>(body)?;
return Err(eyre!(
"\
Status {release_metadata_post_response_status} from metadata POST\n\
{}\
",
String::from_utf8_lossy(&release_metadata_post_response.bytes().await.unwrap())
message
));
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[derive(Debug, thiserror::Error)]
pub(crate) enum Error {
#[error("Unauthorized: {0}")]
Unauthorized(String),
#[error("{upload_name}/{rolling_prefix_or_tag} already exists")]
Conflict {
upload_name: String,
rolling_prefix_or_tag: String,
},
}

impl Error {
pub(crate) fn should_suggest_issue(&self) -> bool {
match self {
Self::Unauthorized(_) | Self::Conflict { .. } => false,
}
}
}
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fmt::Display, io::IsTerminal};

use clap::Parser;
use error::Error;
mod cli;
mod error;
mod flake_info;
mod graphql;
mod release_metadata;
Expand All @@ -18,6 +20,16 @@ async fn main() -> color_eyre::Result<std::process::ExitCode> {
} else {
color_eyre::config::Theme::dark()
})
.issue_filter(|kind| match kind {
color_eyre::ErrorKind::NonRecoverable(_) => true,
color_eyre::ErrorKind::Recoverable(error) => {
if let Some(known_error) = error.downcast_ref::<Error>() {
known_error.should_suggest_issue()
} else {
true
}
}
})
.install()?;

let cli = cli::FlakeHubPushCli::parse();
Expand Down

0 comments on commit e38b538

Please sign in to comment.