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
//! Custom result

use crate::api::{
    config::GearConfig,
    generated::api::{runtime_types::sp_runtime::DispatchError, Event},
};
use subxt::{sp_core::H256, TransactionStatus};

type TxStatus<'t> = TransactionStatus<'t, GearConfig, DispatchError, Event>;

/// transaction error
#[derive(Debug, thiserror::Error)]
pub enum TransactionError {
    #[error("Transaction Retracted( {0} )")]
    Retracted(H256),
    #[error("Transaction Timeout( {0} )")]
    FinalityTimeout(H256),
    #[error("Transaction Usurped( {0} )")]
    Usurped(H256),
    #[error("Transaction Dropped")]
    Dropped,
    #[error("Transaction Invalid")]
    Invalid,
    #[error("Not an error, this will never be reached.")]
    None,
}

impl From<TxStatus<'_>> for Error {
    fn from(status: TxStatus<'_>) -> Self {
        match status {
            TransactionStatus::Retracted(h) => TransactionError::Retracted(h),
            TransactionStatus::FinalityTimeout(h) => TransactionError::FinalityTimeout(h),
            TransactionStatus::Usurped(h) => TransactionError::Usurped(h),
            _ => TransactionError::None,
        }
        .into()
    }
}

/// Errors
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    Anyhow(#[from] anyhow::Error),
    #[error("Invalid node key")]
    BadNodeKey,
    #[error(transparent)]
    Base64Decode(#[from] base64::DecodeError),
    #[error(transparent)]
    Codec(#[from] parity_scale_codec::Error),
    #[error("Code not found {0}")]
    CodeNotFound(String),
    #[error("Could not find directory {0}")]
    CouldNotFindDirectory(String),
    #[error(transparent)]
    Hex(#[from] hex::FromHexError),
    #[error("Unable to get the name of the current executable binary")]
    InvalidExecutable,
    #[error("Password must be provided for logining with json file.")]
    InvalidPassword,
    #[error("Invalid public key")]
    InvalidPublic,
    #[error("Invalid secret key")]
    InvalidSecret,
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    Keyring(#[from] keyring::Error),
    #[error(transparent)]
    Logger(#[from] log::SetLoggerError),
    #[error("No available account was found in keystore, please run `gear login` first.")]
    Logout,
    #[error(transparent)]
    Metadata(#[from] crate::metadata::Error),
    #[error("{0}")]
    Nacl(String),
    #[error("Page {0} of Program {1} was not found in the storage.")]
    PageNotFound(u32, String),
    #[error("Program with id {0} was not found in the storage.")]
    ProgramNotFound(String),
    #[error("Program has been terminated.")]
    ProgramTerminated,
    #[error("{0}")]
    Schnorrkel(schnorrkel::SignatureError),
    #[error(transparent)]
    SerdeJson(#[from] serde_json::Error),
    #[error(transparent)]
    SubxtBasic(#[from] subxt::BasicError),
    #[error(transparent)]
    SubxtGeneric(
        #[from]
        subxt::GenericError<
            subxt::RuntimeError<
                crate::api::generated::api::runtime_types::sp_runtime::DispatchError,
            >,
        >,
    ),
    #[error(transparent)]
    SubxtMetadata(#[from] subxt::MetadataError),
    #[error(transparent)]
    SubxtPublic(#[from] subxt::sp_core::crypto::PublicError),
    #[error(transparent)]
    SubxtRpc(#[from] subxt::rpc::RpcError),
    #[error(transparent)]
    Tx(#[from] TransactionError),
}

impl From<nacl::Error> for Error {
    fn from(err: nacl::Error) -> Self {
        Self::Nacl(err.message)
    }
}

impl From<schnorrkel::SignatureError> for Error {
    fn from(err: schnorrkel::SignatureError) -> Self {
        Self::Schnorrkel(err)
    }
}

/// Custom result
pub type Result<T> = std::result::Result<T, Error>;