Skip to main content

miden_client_cli/
errors.rs

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