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
// This file is part of Gear.
//
// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! GSdk Results
use crate::TxStatus;

/// Transaction Errors
#[derive(Debug, thiserror::Error)]
pub enum TxError {
    #[error("Transaction Error( {0} )")]
    Error(String),
    #[error("Transaction Invalid( {0} )")]
    Invalid(String),
    #[error("Transaction Dropped( {0} )")]
    Dropped(String),
    #[error("Not an error, this will never be reached.")]
    None,
}

impl From<TxStatus> for Error {
    fn from(status: TxStatus) -> Self {
        match status {
            TxStatus::Error { message } => TxError::Error(message),
            TxStatus::Invalid { message } => TxError::Invalid(message),
            TxStatus::Dropped { message } => TxError::Dropped(message),
            unreachable => {
                log::info!("Not an error tx status occurred {unreachable:?}");
                TxError::None
            }
        }
        .into()
    }
}

/// Errors
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    Anyhow(#[from] anyhow::Error),
    #[error(transparent)]
    Base64Decode(#[from] base64::DecodeError),
    #[error(transparent)]
    Codec(#[from] parity_scale_codec::Error),
    #[error("Code not found {0}")]
    CodeNotFound(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("Invalid RPC params")]
    InvalidRpcParams,
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    SerdeJson(#[from] serde_json::Error),
    #[error("The queried storage not found.")]
    StorageNotFound,
    #[error("The queried event not found.")]
    EventNotFound,
    #[error(transparent)]
    Subxt(#[from] subxt::Error),
    #[error(transparent)]
    SubxtCore(#[from] subxt::ext::subxt_core::Error),
    #[error(transparent)]
    SubxtPublic(#[from] sp_core::crypto::PublicError),
    #[error(transparent)]
    SubxtMetadata(#[from] subxt::error::MetadataError),
    #[error(transparent)]
    ScaleValueEncode(#[from] scale_value::scale::EncodeError),
    #[error(transparent)]
    Tx(#[from] TxError),
    #[error(transparent)]
    SubxtRpc(#[from] jsonrpsee::core::ClientError),
    #[error("Page {0} of Program {1} was not found in the storage.")]
    PageNotFound(u32, String),
    #[error("Program has been terminated.")]
    ProgramTerminated,
    #[error("Invalid rpc URL.")]
    InvalidUrl,
    #[error("Page {0} of Program {1} is invalid.")]
    PageInvalid(u32, String),
}

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