1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::net::AddrParseError;

use downcast::DowncastError;
use kaspa_wallet_core::error::Error as WalletError;
use workflow_core::channel::ChannelError;
use workflow_terminal::error::Error as TerminalError;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("{0}")]
    Custom(String),

    #[error("aborting")]
    UserAbort,

    #[error("platform is not supported")]
    Platform,

    #[error(transparent)]
    WalletError(#[from] WalletError),

    #[error("Cli error {0}")]
    TerminalError(#[from] TerminalError),

    #[error("Channel error")]
    ChannelError(String),

    #[error(transparent)]
    WrpcError(#[from] kaspa_wrpc_client::error::Error),

    #[error(transparent)]
    RpcError(#[from] kaspa_rpc_core::RpcError),

    #[error(transparent)]
    SerdeJsonError(#[from] serde_json::Error),

    #[error(transparent)]
    ParseFloatError(#[from] std::num::ParseFloatError),

    #[error(transparent)]
    ParseIntError(#[from] std::num::ParseIntError),

    #[error("invalid hex string: {0}")]
    ParseHexError(#[from] faster_hex::Error),

    #[error(transparent)]
    AddrParseError(#[from] AddrParseError),

    #[error("account '{0}' not found")]
    AccountNotFound(String),

    #[error("ambiguous selection, pattern '{0}' matches too many accounts, please be more specific")]
    AmbiguousAccount(String),

    #[error("please create a wallet")]
    WalletDoesNotExist,

    #[error("please open a wallet")]
    WalletIsNotOpen,

    #[error("unrecognized argument '{0}', accepted arguments are: {1}")]
    UnrecognizedArgument(String, String),

    #[error("multiple matches for argument '{0}'; please be more specific.")]
    MultipleMatches(String),

    #[error("account type must be <bip32|multisig|legacy>")]
    InvalidAccountKind,

    #[error("wallet secret is required")]
    WalletSecretRequired,

    #[error("wallet secrets do not match")]
    WalletSecretMatch,

    #[error("payment secret is required")]
    PaymentSecretRequired,

    #[error("payment secrets do not match")]
    PaymentSecretMatch,

    #[error("key data not found")]
    KeyDataNotFound,

    #[error("no accounts found, please create an account to continue")]
    NoAccounts,

    #[error("no private keys found in this wallet, please create a private key to continue")]
    NoKeys,

    #[error(transparent)]
    AddressError(#[from] kaspa_addresses::AddressError),

    #[error("{0}")]
    DowncastError(String),

    #[error(transparent)]
    Store(#[from] workflow_store::error::Error),

    #[error(transparent)]
    NodeJs(#[from] workflow_node::error::Error),

    #[error(transparent)]
    Daemon(#[from] kaspa_daemon::error::Error),

    #[error(transparent)]
    Dom(#[from] workflow_dom::error::Error),

    #[error(transparent)]
    NetworkId(#[from] kaspa_consensus_core::network::NetworkIdError),

    #[error(transparent)]
    Bip32(#[from] kaspa_bip32::Error),

    #[error("private key {0} already exists")]
    PrivateKeyAlreadyExists(String),

    #[error(transparent)]
    MetricsError(kaspa_metrics_core::error::Error),
}

impl Error {
    pub fn custom<T: Into<String>>(msg: T) -> Self {
        Error::Custom(msg.into())
    }
}

impl From<Error> for TerminalError {
    fn from(e: Error) -> TerminalError {
        TerminalError::Custom(e.to_string())
    }
}

impl<T> From<ChannelError<T>> for Error {
    fn from(e: ChannelError<T>) -> Error {
        Error::ChannelError(e.to_string())
    }
}

impl From<String> for Error {
    fn from(err: String) -> Self {
        Self::Custom(err)
    }
}

impl From<&str> for Error {
    fn from(err: &str) -> Self {
        Self::Custom(err.to_string())
    }
}

impl<T> From<DowncastError<T>> for Error {
    fn from(e: DowncastError<T>) -> Self {
        Error::DowncastError(e.to_string())
    }
}