ic_web3_rs/contract/
error.rs

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
//! Contract call/query error.

use crate::error::Error as ApiError;
use derive_more::{Display, From};
use ethabi::Error as EthError;

/// Contract error.
#[derive(Debug, Display, From)]
pub enum Error {
    /// invalid output type requested by the caller
    #[display(fmt = "Invalid output type: {}", _0)]
    InvalidOutputType(String),
    /// eth abi error
    #[display(fmt = "Abi error: {}", _0)]
    Abi(EthError),
    /// Rpc error
    #[display(fmt = "Api error: {}", _0)]
    Api(ApiError),
    /// An error during deployment.
    //#[display(fmt = "Deployment error: {}", _0)]
    //Deployment(crate::contract::deploy::Error),
    /// Contract does not support this interface.
    InterfaceUnsupported,
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            Error::InvalidOutputType(_) => None,
            Error::Abi(ref e) => Some(e),
            Error::Api(ref e) => Some(e),
            //Error::Deployment(ref e) => Some(e),
            Error::InterfaceUnsupported => None,
        }
    }
}

impl From<Error> for crate::error::Error {
    fn from(e: Error) -> Self {
        match e {
            Error::InvalidOutputType(s) => crate::error::Error::InvalidResponse(s),
            Error::Abi(eth_error) => crate::error::Error::Decoder(format!("{}", eth_error)),
            Error::Api(api_error) => api_error,
            Error::InterfaceUnsupported => crate::error::Error::Internal,
        }
    }
}

pub mod deploy {
    use crate::{error::Error as ApiError, types::H256};
    use derive_more::{Display, From};

    /// Contract deployment error.
    #[derive(Debug, Display, From)]
    pub enum Error {
        /// eth abi error
        #[display(fmt = "Abi error: {}", _0)]
        Abi(ethabi::Error),
        /// Rpc error
        #[display(fmt = "Api error: {}", _0)]
        Api(ApiError),
        /// Contract deployment failed
        #[display(fmt = "Failure during deployment.Tx hash: {:?}", _0)]
        ContractDeploymentFailure(H256),
    }

    impl std::error::Error for Error {
        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
            match *self {
                Error::Abi(ref e) => Some(e),
                Error::Api(ref e) => Some(e),
                Error::ContractDeploymentFailure(_) => None,
            }
        }
    }
}