tendermint_proto_althea/
error.rs

1//! This module defines the various errors that be raised during Protobuf conversions.
2
3use flex_error::{define_error, DisplayOnly};
4use prost::{DecodeError, EncodeError};
5use std::convert::TryFrom;
6use std::fmt::Display;
7use std::num::TryFromIntError;
8
9define_error! {
10    Error {
11        TryFromProtobuf
12            { reason: String }
13            | e | {
14                format!("error converting message type into domain type: {}",
15                    e.reason)
16            },
17
18        EncodeMessage
19            [ DisplayOnly<EncodeError> ]
20            | _ | { "error encoding message into buffer" },
21
22        DecodeMessage
23            [ DisplayOnly<DecodeError> ]
24            | _ | { "error decoding buffer into message" },
25
26        ParseLength
27            [ DisplayOnly<TryFromIntError> ]
28            | _ | { "error parsing encoded length" },
29    }
30}
31
32impl Error {
33    pub fn try_from<Raw, T, E>(e: E) -> Error
34    where
35        E: Display,
36        T: TryFrom<Raw, Error = E>,
37    {
38        Error::try_from_protobuf(format!("{}", e))
39    }
40}