Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed Apr 22, 2024
1 parent 7a9e918 commit 9221bb8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 140 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ members = ["crates/core", "crates/wit"]

[workspace.dependencies]
cargo-component-core = { path = "crates/core", version = "0.11.0" }
warg-protocol = { git = "https://github.com/bytecodealliance/registry", branch = "namespace-enhancements" }
warg-crypto = { git = "https://github.com/bytecodealliance/registry", branch = "namespace-enhancements" }
warg-client = { git = "https://github.com/bytecodealliance/registry", branch = "namespace-enhancements" }
warg-credentials = { git = "https://github.com/bytecodealliance/registry", branch = "namespace-enhancements" }
warg-server = { git = "https://github.com/bytecodealliance/registry", branch = "namespace-enhancements" }
warg-protocol = { git = "https://github.com/bytecodealliance/registry", rev = "fd8c963" }
warg-crypto = { git = "https://github.com/bytecodealliance/registry", rev = "fd8c963" }
warg-client = { git = "https://github.com/bytecodealliance/registry", rev = "fd8c963" }
warg-credentials = { git = "https://github.com/bytecodealliance/registry", rev = "fd8c963" }
warg-server = { git = "https://github.com/bytecodealliance/registry", rev = "fd8c963" }
anyhow = "1.0.82"
clap = { version = "4.5.4", features = ["derive"] }
toml_edit = { version = "0.22.9", features = ["serde"] }
Expand Down
53 changes: 14 additions & 39 deletions crates/core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,14 @@ use wit_parser::{PackageId, PackageName, Resolve, UnresolvedPackage, WorldId};
/// Error for CLI commands
pub enum CommandError {
/// General errors
#[error("Error: `{0}`")]
General(anyhow::Error),
#[error(transparent)]
General(#[from] anyhow::Error),
/// Client Error
#[error("Warg Client Error: {0}")]
WargClient(ClientError),
#[error("warg client error ({0}): {1}")]
WargClient(String, ClientError),
/// Client Error With Hint
#[error("Warg Client Error: {0}")]
WargHint(ClientError),
}

/// Error from warg client
pub struct WargClientError(pub ClientError);

impl std::fmt::Debug for WargClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("WargClientError").field(&self.0).finish()
}
}

impl From<anyhow::Error> for CommandError {
fn from(value: anyhow::Error) -> Self {
CommandError::General(value)
}
}

impl From<WargClientError> for CommandError {
fn from(value: WargClientError) -> Self {
match &value.0 {
ClientError::PackageDoesNotExistWithHint { .. } => {
CommandError::WargHint(value.0.into())
}
_ => CommandError::WargClient(value.0.into()),
}
}
#[error("warg client error with hint ({0}): {1}")]
WargHint(String, ClientError),
}

/// The name of the default registry.
Expand Down Expand Up @@ -553,8 +527,7 @@ impl<'a> DependencyResolver<'a> {

registry
.add_dependency(name, package_name, &package.version, registry_name, locked)
.await
.map_err(|e| WargClientError(e.into()))?;
.await?
}
Dependency::Local(p) => {
// A local path dependency, insert a resolution immediately
Expand Down Expand Up @@ -639,10 +612,12 @@ impl<'a> DependencyResolver<'a> {
futures.push(tokio::spawn(async move {
(
index,
client
.upsert(upserts.iter())
.await
.map_err(|e| WargClientError(e)),
client.upsert(upserts.iter()).await.map_err(|e| match &e {
ClientError::PackageDoesNotExistWithHint { .. } => {
CommandError::WargHint("error updating warg logs".to_string(), e)
}
_ => CommandError::WargClient("error updating warg logs".to_string(), e),
}),
)
}))
}
Expand All @@ -656,7 +631,7 @@ impl<'a> DependencyResolver<'a> {
.get_index_mut(index)
.expect("out of bounds registry index");

res.map_err(|e| CommandError::from(e))?;
res?;

log::info!("package logs successfully updated for component registry `{name}`");
finished += 1;
Expand Down
123 changes: 57 additions & 66 deletions crates/wit/src/bin/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn main() -> Result<()> {

with_interactive_retry(|retry: Option<Retry>| async {
let app = Wit::parse();
if let Err(e) = match app.command {
if let Err(err) = match app.command {
Command::Init(cmd) => cmd.exec(),
Command::Add(cmd) => cmd.exec(retry).await,
Command::Build(cmd) => cmd.exec(retry).await,
Expand All @@ -54,74 +54,65 @@ async fn main() -> Result<()> {
Command::Update(cmd) => cmd.exec(retry).await,
}
{
match e {
CommandError::General(e) => {
let terminal = Terminal::new(Verbosity::Normal, Color::Auto);
terminal.error(e)?;
exit(1);
}
CommandError::WargClient(e) => {
let terminal = Terminal::new(Verbosity::Normal, Color::Auto);
terminal.error(e)?;
exit(1);
}
CommandError::WargHint(e) => {
if let ClientError::PackageDoesNotExistWithHint { name, hint } = e {
let hint_reg = hint.to_str().unwrap();
let mut terms = hint_reg.split('=');
let namespace = terms.next();
let registry = terms.next();
if let (Some(namespace), Some(registry)) = (namespace, registry) {
let prompt = format!(
"The package `{}`, does not exist in the registry you're using.\nHowever, the package namespace `{namespace}` does exist in the registry at {registry}.\nWould you like to configure your warg cli to use this registry for packages with this namespace in the future? y/N\n",
name.name()
);
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.interact()
.unwrap()
{
if let Err(e) = match Wit::parse().command {
Command::Init(cmd) => cmd.exec(),
Command::Add(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
Command::Build(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
Command::Publish(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
Command::Key(cmd) => cmd.exec().await,
Command::Update(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
} {
let terminal = Terminal::new(Verbosity::Normal, Color::Auto);
terminal.error(e)?;
exit(1);
}
}
if let CommandError::WargHint(_, ClientError::PackageDoesNotExistWithHint { name, hint }) = &err {
if let Some((namespace, registry)) = hint.to_str().unwrap().split_once('=') {
let prompt = format!(
"The package `{}`, does not exist in the registry you're using.
However, the package namespace `{namespace}` does exist in the registry at {registry}.
Would you like to configure your warg cli to use this registry for packages with this namespace in the future? y/N\n",
name.name(),
);
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.interact()
.unwrap()
{
if let Err(e) = match Wit::parse().command {
Command::Init(cmd) => cmd.exec(),
Command::Add(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
Command::Build(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
Command::Publish(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
Command::Key(cmd) => cmd.exec().await,
Command::Update(cmd) => {
cmd.exec(Some(Retry::new(
namespace.to_string(),
registry.to_string(),
)))
.await
}
} {
let terminal = Terminal::new(Verbosity::Normal, Color::Auto);
terminal.error(e)?;
exit(1);
} else {
return Ok(())
}
}
}
}
}
else {
let terminal = Terminal::new(Verbosity::Normal, Color::Auto);
terminal.error(err)?;
exit(1);
}
}
Ok(())
}).await?;
Expand Down
19 changes: 14 additions & 5 deletions crates/wit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use bytes::Bytes;
use cargo_component_core::{
lock::{LockFile, LockFileResolver, LockedPackage, LockedPackageVersion},
registry::{
create_client, CommandError, DecodedDependency, DependencyResolutionMap,
DependencyResolver, WargClientError,
create_client, CommandError, DecodedDependency, DependencyResolutionMap, DependencyResolver,
},
terminal::{Colors, Terminal},
};
Expand All @@ -18,7 +17,7 @@ use lock::{acquire_lock_file_ro, acquire_lock_file_rw, to_lock_file};
use std::{collections::HashSet, path::Path, time::Duration};
use warg_client::{
storage::{ContentStorage, PublishEntry, PublishInfo},
Retry,
ClientError, Retry,
};
use warg_crypto::signing::PrivateKey;
use warg_protocol::registry;
Expand Down Expand Up @@ -368,12 +367,22 @@ async fn publish_wit_package(
let record_id = client
.publish_with_info(options.signing_key, info)
.await
.map_err(|e| WargClientError(e))?;
.map_err(|e| match &e {
ClientError::PackageDoesNotExistWithHint { .. } => {
CommandError::WargHint("error publishing package".to_string(), e)
}
_ => CommandError::WargClient("error publishing package".to_string(), e),
})?;

client
.wait_for_publish(name, &record_id, Duration::from_secs(1))
.await
.map_err(|e| WargClientError(e))?;
.map_err(|e| match &e {
ClientError::PackageDoesNotExistWithHint { .. } => {
CommandError::WargHint("error publishing package".to_string(), e)
}
_ => CommandError::WargClient("error publishing package".to_string(), e),
})?;

terminal.status(
"Published",
Expand Down
Loading

0 comments on commit 9221bb8

Please sign in to comment.