ibc_query/
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
77
78
79
80
81
82
83
84
use alloc::string::{String, ToString};

use displaydoc::Display;
use ibc::core::channel::types::error::ChannelError;
use ibc::core::client::types::error::ClientError;
use ibc::core::connection::types::error::ConnectionError;
use ibc::core::handler::types::error::HandlerError;
use ibc::core::host::types::error::{DecodingError, HostError, IdentifierError};
use tonic::Status;

/// The main error type of the ibc-query crate. This type mainly
/// serves to surface lower-level errors that occur when executing
/// ibc-query's codepaths.
#[derive(Debug, Display)]
pub enum QueryError {
    /// handler error: `{0}`
    Handler(HandlerError),
    /// host error: `{0}`
    Host(HostError),
    /// decoding error: `{0}`
    Decoding(DecodingError),
    /// missing proof: `{0}`
    MissingProof(String),
    /// missing field: `{0}`
    MissingField(String),
}

impl QueryError {
    pub fn missing_proof<T: ToString>(description: T) -> Self {
        Self::MissingProof(description.to_string())
    }

    pub fn missing_field<T: ToString>(description: T) -> Self {
        Self::MissingField(description.to_string())
    }
}

impl From<QueryError> for Status {
    fn from(e: QueryError) -> Self {
        match e {
            QueryError::Handler(ctx_err) => Self::internal(ctx_err.to_string()),
            QueryError::Host(host_err) => Self::internal(host_err.to_string()),
            QueryError::Decoding(de) => Self::internal(de.to_string()),
            QueryError::MissingProof(description) => Self::not_found(description),
            QueryError::MissingField(description) => Self::invalid_argument(description),
        }
    }
}

impl From<ClientError> for QueryError {
    fn from(e: ClientError) -> Self {
        Self::Handler(HandlerError::Client(e))
    }
}

impl From<ConnectionError> for QueryError {
    fn from(e: ConnectionError) -> Self {
        Self::Handler(HandlerError::Connection(e))
    }
}

impl From<ChannelError> for QueryError {
    fn from(e: ChannelError) -> Self {
        Self::Handler(HandlerError::Channel(e))
    }
}

impl From<DecodingError> for QueryError {
    fn from(e: DecodingError) -> Self {
        Self::Decoding(e)
    }
}

impl From<IdentifierError> for QueryError {
    fn from(e: IdentifierError) -> Self {
        Self::Decoding(DecodingError::Identifier(e))
    }
}

impl From<HostError> for QueryError {
    fn from(e: HostError) -> Self {
        Self::Host(e)
    }
}