Skip to content

Commit

Permalink
Refactor error handling across CLI commands
Browse files Browse the repository at this point in the history
Standardized the usage of the `anyhow` crate for error handling and improved error reporting. Additionally, the main function now exits with a failure status upon encountering an error.
  • Loading branch information
steebchen committed Sep 18, 2024
1 parent c5af3f6 commit 278bee6
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cli/src/command/auth/info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use slot::graphql::auth::{me::*, Me};
use slot::graphql::{GraphQLQuery, Response};
Expand All @@ -18,7 +18,7 @@ impl InfoArgs {

if let Some(errors) = res.errors {
for err in errors {

Check failure on line 20 in cli/src/command/auth/info.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}
return Ok(());
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/auth/login.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use anyhow::Result;
use anyhow::{anyhow, Result};
use axum::{
extract::{Query, State},
response::{IntoResponse, Redirect, Response},
Expand Down Expand Up @@ -120,7 +120,7 @@ async fn handler(
// display the errors if any, but still process bcs we have the token
if let Some(errors) = res.errors {
for err in errors {

Check failure on line 122 in cli/src/command/auth/login.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
eprintln!("Error: {}", err.message);
return Err(CallbackError::Other(anyhow!(err.message)));
}
}

Expand Down
12 changes: 6 additions & 6 deletions cli/src/command/deployments/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::enum_variant_names)]

use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use katana_primitives::contract::ContractAddress;
use katana_primitives::genesis::allocation::GenesisAccountAlloc;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl AccountsArgs {
let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 43 in cli/src/command/deployments/accounts.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}
}

Expand All @@ -57,17 +57,17 @@ impl AccountsArgs {
FieldElement::from_bytes_be(

Check warning on line 57 in cli/src/command/deployments/accounts.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/slot/slot/cli/src/command/deployments/accounts.rs
&Felt::from_hex(&account.address).unwrap().to_bytes_be(),
)
.unwrap(),
.unwrap(),
);

let public_key = FieldElement::from_bytes_be(
&Felt::from_hex(&account.public_key).unwrap().to_bytes_be(),

Check warning on line 64 in cli/src/command/deployments/accounts.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/slot/slot/cli/src/command/deployments/accounts.rs
)
.unwrap();
.unwrap();
let private_key = FieldElement::from_bytes_be(
&Felt::from_hex(&account.private_key).unwrap().to_bytes_be(),
)
.unwrap();
.unwrap();

Check warning on line 70 in cli/src/command/deployments/accounts.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/slot/slot/cli/src/command/deployments/accounts.rs

let genesis_account = GenesisAccount {
public_key,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl AccountsArgs {

Check warning on line 109 in cli/src/command/deployments/accounts.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/slot/slot/cli/src/command/deployments/accounts.rs
fn print_genesis_accounts<'a, Accounts>(accounts: Accounts, seed: Option<&str>)
where
Accounts: Iterator<Item = (&'a ContractAddress, &'a GenesisAccountAlloc)>,
Accounts: Iterator<Item=(&'a ContractAddress, &'a GenesisAccountAlloc)>,
{
println!(
r"
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/deployments/create.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::enum_variant_names)]

use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use slot::api::Client;
use slot::credential::Credentials;
Expand Down Expand Up @@ -143,7 +143,7 @@ impl CreateArgs {
let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 145 in cli/src/command/deployments/create.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}

return Ok(());
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/deployments/delete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
Expand Down Expand Up @@ -64,7 +64,7 @@ impl DeleteArgs {
let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 66 in cli/src/command/deployments/delete.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/deployments/describe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::enum_variant_names)]

use super::services::Service;
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use slot::graphql::deployments::describe_deployment::DescribeDeploymentDeploymentConfig::{
KatanaConfig, MadaraConfig, SayaConfig, ToriiConfig,
Expand Down Expand Up @@ -40,7 +40,7 @@ impl DescribeArgs {
let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 42 in cli/src/command/deployments/describe.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/deployments/fork.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::enum_variant_names)]

use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use slot::graphql::deployments::fork_deployment::ForkDeploymentForkDeployment::KatanaConfig;
use slot::graphql::deployments::{fork_deployment::*, ForkDeployment};
Expand Down Expand Up @@ -50,7 +50,7 @@ impl ForkArgs {
let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 52 in cli/src/command/deployments/fork.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/deployments/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::enum_variant_names)]

use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;

use slot::graphql::deployments::list_deployments::{ResponseData, Variables};
Expand All @@ -22,7 +22,7 @@ impl ListArgs {
let res: Response<ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 24 in cli/src/command/deployments/list.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/deployments/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::services::UpdateServiceCommands;
use crate::command::deployments::Tier;
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use slot::api::Client;
use slot::credential::Credentials;
Expand Down Expand Up @@ -78,7 +78,7 @@ impl UpdateArgs {
let res: Response<update_deployment::ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 80 in cli/src/command/deployments/update.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}

return Ok(());
Expand Down
8 changes: 4 additions & 4 deletions cli/src/command/teams/members.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Args;
use slot::api::Client;
use slot::credential::Credentials;
Expand All @@ -23,7 +23,7 @@ impl TeamListArgs {
let res: Response<team_members_list::ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {

Check failure on line 25 in cli/src/command/teams/members.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop never actually loops
println!("Error: {}", err.message);
return Err(anyhow!(err));
}

return Ok(());
Expand Down Expand Up @@ -66,7 +66,7 @@ impl TeamAddArgs {
let res: Response<team_member_add::ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors {
for err in errors {
println!("Error: {}", err.message);
return Err(anyhow!(err));
}

return Ok(());
Expand Down Expand Up @@ -96,7 +96,7 @@ impl TeamRemoveArgs {
let res: Response<team_member_remove::ResponseData> = client.query(&request_body).await?;
if let Some(errors) = res.errors {
for err in errors {
println!("Error: {}", err.message);
return Err(anyhow!(err));
}

return Ok(());
Expand Down
3 changes: 2 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ async fn main() {
match &cli.command.run().await {
Ok(_) => {}
Err(e) => {
error!("{e}")
error!("{e}");
std::process::exit(1);
}
}
}

0 comments on commit 278bee6

Please sign in to comment.