ic_web3_rs/contract/
error.rs

1//! Contract call/query error.
2
3use crate::error::Error as ApiError;
4use derive_more::{Display, From};
5use ethabi::Error as EthError;
6
7/// Contract error.
8#[derive(Debug, Display, From)]
9pub enum Error {
10    /// invalid output type requested by the caller
11    #[display(fmt = "Invalid output type: {}", _0)]
12    InvalidOutputType(String),
13    /// eth abi error
14    #[display(fmt = "Abi error: {}", _0)]
15    Abi(EthError),
16    /// Rpc error
17    #[display(fmt = "Api error: {}", _0)]
18    Api(ApiError),
19    /// An error during deployment.
20    //#[display(fmt = "Deployment error: {}", _0)]
21    //Deployment(crate::contract::deploy::Error),
22    /// Contract does not support this interface.
23    InterfaceUnsupported,
24}
25
26impl std::error::Error for Error {
27    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28        match *self {
29            Error::InvalidOutputType(_) => None,
30            Error::Abi(ref e) => Some(e),
31            Error::Api(ref e) => Some(e),
32            //Error::Deployment(ref e) => Some(e),
33            Error::InterfaceUnsupported => None,
34        }
35    }
36}
37
38impl From<Error> for crate::error::Error {
39    fn from(e: Error) -> Self {
40        match e {
41            Error::InvalidOutputType(s) => crate::error::Error::InvalidResponse(s),
42            Error::Abi(eth_error) => crate::error::Error::Decoder(format!("{}", eth_error)),
43            Error::Api(api_error) => api_error,
44            Error::InterfaceUnsupported => crate::error::Error::Internal,
45        }
46    }
47}
48
49pub mod deploy {
50    use crate::{error::Error as ApiError, types::H256};
51    use derive_more::{Display, From};
52
53    /// Contract deployment error.
54    #[derive(Debug, Display, From)]
55    pub enum Error {
56        /// eth abi error
57        #[display(fmt = "Abi error: {}", _0)]
58        Abi(ethabi::Error),
59        /// Rpc error
60        #[display(fmt = "Api error: {}", _0)]
61        Api(ApiError),
62        /// Contract deployment failed
63        #[display(fmt = "Failure during deployment.Tx hash: {:?}", _0)]
64        ContractDeploymentFailure(H256),
65    }
66
67    impl std::error::Error for Error {
68        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69            match *self {
70                Error::Abi(ref e) => Some(e),
71                Error::Api(ref e) => Some(e),
72                Error::ContractDeploymentFailure(_) => None,
73            }
74        }
75    }
76}