Skip to content

Commit

Permalink
feat: print error details
Browse files Browse the repository at this point in the history
Certain JSON-RPC errors come with additional context. These additional
data are now properly displayed.
  • Loading branch information
xJonathanLEI committed Jan 10, 2024
1 parent 491eb81 commit 260ea7a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
51 changes: 51 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::fmt::Display;

use starknet::{
accounts::{AccountError, AccountFactoryError},
core::types::StarknetError,
providers::ProviderError,
};

/// Makes error details visible, as they're not displayed by default.
pub fn account_error_mapper<S>(err: AccountError<S>) -> anyhow::Error
where
S: Display,
{
match err {
AccountError::Provider(ProviderError::StarknetError(err)) => map_starknet_error(err),
err => anyhow::anyhow!("{}", err),
}
}

/// Makes error details visible, as they're not displayed by default.
pub fn account_factory_error_mapper<S>(err: AccountFactoryError<S>) -> anyhow::Error
where
S: Display,
{
match err {
AccountFactoryError::Provider(ProviderError::StarknetError(err)) => map_starknet_error(err),
err => anyhow::anyhow!("{}", err),
}
}

fn map_starknet_error(err: StarknetError) -> anyhow::Error {
match err {
StarknetError::ContractError(err) => {
anyhow::anyhow!("ContractError: {}", err.revert_error.trim())
}
StarknetError::TransactionExecutionError(err) => {
anyhow::anyhow!(
"TransactionExecutionError (tx index {}): {}",
err.transaction_index,
err.execution_error.trim()
)
}
StarknetError::ValidationFailure(err) => {
anyhow::anyhow!("ValidationFailure: {}", err.trim())
}
StarknetError::UnexpectedError(err) => {
anyhow::anyhow!("UnexpectedError: {}", err.trim())
}
err => anyhow::anyhow!("{}", err),
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod casm;
mod chain_id;
mod compiler;
mod decode;
mod error;
mod fee;
mod network;
mod path;
Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
DeploymentContext, DeploymentStatus,
},
account_factory::{AnyAccountFactory, BraavosAccountFactory},
error::account_factory_error_mapper,
fee::{FeeArgs, FeeSetting},
path::ExpandedPathbufParser,
signer::SignerArgs,
Expand Down Expand Up @@ -201,7 +202,11 @@ impl Deploy {
let max_fee = match fee_setting {
FeeSetting::Manual(fee) => MaxFeeType::Manual { max_fee: fee },
FeeSetting::EstimateOnly | FeeSetting::None => {
let estimated_fee = account_deployment.estimate_fee().await?.overall_fee;
let estimated_fee = account_deployment
.estimate_fee()
.await
.map_err(account_factory_error_mapper)?
.overall_fee;

// TODO: make buffer configurable
let estimated_fee_with_buffer = (estimated_fee * felt!("3")).floor_div(felt!("2"));
Expand Down
13 changes: 11 additions & 2 deletions src/subcommands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use starknet::{
use crate::{
account::AccountArgs,
casm::{CasmArgs, CasmHashSource},
error::account_error_mapper,
fee::{FeeArgs, FeeSetting},
path::ExpandedPathbufParser,
utils::watch_tx,
Expand Down Expand Up @@ -144,7 +145,11 @@ impl Declare {
let max_fee = match fee_setting {
FeeSetting::Manual(fee) => fee,
FeeSetting::EstimateOnly | FeeSetting::None => {
let estimated_fee = declaration.estimate_fee().await?.overall_fee;
let estimated_fee = declaration
.estimate_fee()
.await
.map_err(account_error_mapper)?
.overall_fee;

if fee_setting.is_estimate_only() {
println!(
Expand Down Expand Up @@ -207,7 +212,11 @@ impl Declare {
let max_fee = match fee_setting {
FeeSetting::Manual(fee) => fee,
FeeSetting::EstimateOnly | FeeSetting::None => {
let estimated_fee = declaration.estimate_fee().await?.overall_fee;
let estimated_fee = declaration
.estimate_fee()
.await
.map_err(account_error_mapper)?
.overall_fee;

if fee_setting.is_estimate_only() {
println!(
Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
account::AccountArgs,
address_book::AddressBookResolver,
decode::FeltDecoder,
error::account_error_mapper,
fee::{FeeArgs, FeeSetting},
utils::watch_tx,
verbosity::VerbosityArgs,
Expand Down Expand Up @@ -94,7 +95,11 @@ impl Deploy {
let max_fee = match fee_setting {
FeeSetting::Manual(fee) => fee,
FeeSetting::EstimateOnly | FeeSetting::None => {
let estimated_fee = contract_deployment.estimate_fee().await?.overall_fee;
let estimated_fee = contract_deployment
.estimate_fee()
.await
.map_err(account_error_mapper)?
.overall_fee;

if fee_setting.is_estimate_only() {
eprintln!(
Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
account::AccountArgs,
address_book::AddressBookResolver,
decode::FeltDecoder,
error::account_error_mapper,
fee::{FeeArgs, FeeSetting},
utils::watch_tx,
verbosity::VerbosityArgs,
Expand Down Expand Up @@ -108,7 +109,11 @@ impl Invoke {
let max_fee = match fee_setting {
FeeSetting::Manual(fee) => fee,
FeeSetting::EstimateOnly | FeeSetting::None => {
let estimated_fee = execution.estimate_fee().await?.overall_fee;
let estimated_fee = execution
.estimate_fee()
.await
.map_err(account_error_mapper)?
.overall_fee;

if fee_setting.is_estimate_only() {
println!(
Expand Down

0 comments on commit 260ea7a

Please sign in to comment.