Skip to main content

miden_client_cli/
errors.rs

1use std::error::Error;
2
3use miden_client::account::{AccountId, AddressError};
4use miden_client::keystore::KeyStoreError;
5use miden_client::{
6    AccountError,
7    AccountIdError,
8    AssetError,
9    ClientError,
10    CodeBuilderError,
11    ErrorHint,
12    NetworkIdError,
13};
14use miette::Diagnostic;
15use thiserror::Error;
16
17use crate::client_binary_name;
18type SourceError = Box<dyn Error + Send + Sync>;
19
20#[derive(Debug, Diagnostic, Error)]
21pub enum CliError {
22    #[error("account error: {1}")]
23    #[diagnostic(code(cli::account_error))]
24    Account(#[source] AccountError, String),
25    #[error("account component error: {1}")]
26    #[diagnostic(code(cli::account_error))]
27    AccountComponentError(#[source] SourceError, String),
28    #[error("account id error: {1}")]
29    #[diagnostic(code(cli::accountid_error), help("Check the account ID format."))]
30    AccountId(#[source] AccountIdError, String),
31    #[error("address error: {1}")]
32    #[diagnostic(code(cli::address_error), help("Check the address format."))]
33    Address(#[source] AddressError, String),
34    #[error("asset error")]
35    #[diagnostic(code(cli::asset_error))]
36    Asset(#[source] AssetError),
37    #[error("client error: {error}")]
38    #[diagnostic(code(cli::client_error))]
39    Client {
40        #[source]
41        #[allow(unused_assignments)]
42        error: ClientError,
43        #[help]
44        help: Option<String>,
45    },
46    #[error("config error: {1}")]
47    #[diagnostic(
48        code(cli::config_error),
49        help(
50            "Check if the configuration file exists and is well-formed. If it does not exist, run `{} init` command to create it.",
51            client_binary_name().display()
52
53        )
54    )]
55    Config(#[source] SourceError, String),
56    #[error("configuration file not found: {0}")]
57    #[diagnostic(
58        code(cli::config_not_found),
59        help(
60            "Run `{} init` command to create a configuration file.",
61            client_binary_name().display()
62        )
63    )]
64    ConfigNotFound(String),
65    #[error("execute program error: {1}")]
66    #[diagnostic(code(cli::execute_program_error))]
67    Exec(#[source] SourceError, String),
68    #[error("export error: {0}")]
69    #[diagnostic(code(cli::export_error), help("Check the ID."))]
70    Export(String),
71    #[error("faucet error: {0}")]
72    #[diagnostic(code(cli::faucet_error))]
73    Faucet(String),
74    #[error("import error: {0}")]
75    #[diagnostic(code(cli::import_error), help("Check the file name."))]
76    Import(String),
77    #[error("init data error: {1}")]
78    #[diagnostic(code(cli::account_error))]
79    InitDataError(#[source] SourceError, String),
80    #[error("input error: {0}")]
81    #[diagnostic(code(cli::input_error))]
82    Input(String),
83    #[error("io error")]
84    #[diagnostic(code(cli::io_error))]
85    IO(#[from] std::io::Error),
86    #[error("internal error")]
87    Internal(#[source] SourceError),
88    #[error("keystore error")]
89    #[diagnostic(code(cli::keystore_error))]
90    KeyStore(#[source] KeyStoreError),
91    #[error("missing flag: {0}")]
92    #[diagnostic(code(cli::config_error), help("Check the configuration file format."))]
93    MissingFlag(String),
94    #[error("network id error")]
95    NetworkIdError(#[from] NetworkIdError),
96    #[error("invalid argument: {0}")]
97    InvalidArgument(String),
98    #[error("parse error: {1}")]
99    #[diagnostic(code(cli::parse_error), help("Check the inputs."))]
100    Parse(#[source] SourceError, String),
101    #[error("script builder error")]
102    #[diagnostic(code(cli::script_builder_error))]
103    CodeBuilder(#[from] CodeBuilderError),
104    #[error("transaction error: {1}")]
105    #[diagnostic(code(cli::transaction_error))]
106    Transaction(#[source] SourceError, String),
107    #[error("expected full account, but got partial account: {0}")]
108    InvalidAccount(AccountId),
109}
110
111impl From<ClientError> for CliError {
112    fn from(error: ClientError) -> Self {
113        let help = Option::<ErrorHint>::from(&error).map(ErrorHint::into_help_message);
114        CliError::Client { error, help }
115    }
116}