test_tube/runner/
error.rs

1use cosmrs::rpc::error::Error as TendermintRpcError;
2use cosmrs::tendermint::Error as TendermintError;
3use cosmrs::ErrorReport;
4use std::str::Utf8Error;
5use thiserror::Error;
6
7#[derive(Error, Debug)]
8pub enum RunnerError {
9    #[error("unable to encode request")]
10    EncodeError(#[from] EncodeError),
11
12    #[error("unable to decode response")]
13    DecodeError(#[from] DecodeError),
14
15    #[error("query error: {}", .msg)]
16    QueryError { msg: String },
17
18    #[error("execute error: {}", .msg)]
19    ExecuteError { msg: String },
20
21    #[error("{0}")]
22    GenericError(String),
23
24    #[error("{0}")]
25    ErrorReport(#[from] ErrorReport),
26
27    #[error("{0}")]
28    Tendermint(#[from] TendermintError),
29
30    #[error("{0}")]
31    TendermintRpc(#[from] TendermintRpcError),
32}
33
34impl PartialEq for RunnerError {
35    fn eq(&self, other: &Self) -> bool {
36        match (self, other) {
37            (RunnerError::EncodeError(a), RunnerError::EncodeError(b)) => a == b,
38            (RunnerError::DecodeError(a), RunnerError::DecodeError(b)) => a == b,
39            (RunnerError::QueryError { msg: a }, RunnerError::QueryError { msg: b }) => a == b,
40            (RunnerError::ExecuteError { msg: a }, RunnerError::ExecuteError { msg: b }) => a == b,
41            (RunnerError::ErrorReport(a), RunnerError::ErrorReport(b)) => {
42                a.to_string() == b.to_string()
43            }
44            (RunnerError::Tendermint(a), RunnerError::Tendermint(b)) => {
45                a.to_string() == b.to_string()
46            }
47            (RunnerError::TendermintRpc(a), RunnerError::TendermintRpc(b)) => a.0 == b.0,
48            _ => false,
49        }
50    }
51}
52
53#[derive(Error, Debug)]
54pub enum DecodeError {
55    #[error("invalid utf8 bytes")]
56    Utf8Error(#[from] Utf8Error),
57
58    #[error("invalid protobuf")]
59    ProtoDecodeError(#[from] prost::DecodeError),
60
61    #[error("invalid json")]
62    JsonDecodeError(#[from] serde_json::Error),
63
64    #[error("invalid base64")]
65    Base64DecodeError(#[from] base64::DecodeError),
66
67    #[error("invalid signing key")]
68    SigningKeyDecodeError { msg: String },
69}
70
71impl PartialEq for DecodeError {
72    fn eq(&self, other: &Self) -> bool {
73        match (self, other) {
74            (DecodeError::Utf8Error(a), DecodeError::Utf8Error(b)) => a == b,
75            (DecodeError::ProtoDecodeError(a), DecodeError::ProtoDecodeError(b)) => a == b,
76            (DecodeError::JsonDecodeError(a), DecodeError::JsonDecodeError(b)) => {
77                a.to_string() == b.to_string()
78            }
79            (DecodeError::Base64DecodeError(a), DecodeError::Base64DecodeError(b)) => a == b,
80            (
81                DecodeError::SigningKeyDecodeError { msg: a },
82                DecodeError::SigningKeyDecodeError { msg: b },
83            ) => a == b,
84            _ => false,
85        }
86    }
87}
88
89#[derive(Error, Debug)]
90pub enum EncodeError {
91    #[error("invalid protobuf")]
92    ProtoEncodeError(#[from] prost::EncodeError),
93
94    #[error("unable to encode json")]
95    JsonEncodeError(#[from] serde_json::Error),
96}
97
98impl EncodeError {
99    pub fn from_proto_error_report(err: cosmrs::ErrorReport) -> Self {
100        match err.downcast::<prost::EncodeError>() {
101            Ok(encode_err) => EncodeError::ProtoEncodeError(encode_err),
102            Err(e) => panic!("expect `prost::EncodeError` but got {:?}", e),
103        }
104    }
105}
106
107impl PartialEq for EncodeError {
108    fn eq(&self, other: &Self) -> bool {
109        match (self, other) {
110            (EncodeError::ProtoEncodeError(a), EncodeError::ProtoEncodeError(b)) => a == b,
111            (EncodeError::JsonEncodeError(a), EncodeError::JsonEncodeError(b)) => {
112                a.to_string() == b.to_string()
113            }
114            _ => false,
115        }
116    }
117}