tenderdash_proto/
error.rs

1//! This module defines the various errors that be raised during Protobuf
2//! conversions.
3#[cfg(not(feature = "std"))]
4use core::{convert::TryFrom, fmt::Display, num::TryFromIntError};
5#[cfg(feature = "std")]
6use std::{fmt::Display, num::TryFromIntError};
7
8use prost::{DecodeError, EncodeError};
9use thiserror::Error as ThisError;
10
11use crate::prelude::*;
12
13#[derive(Debug, ThisError)]
14pub enum Error {
15    #[error("error converting time: {reason}")]
16    TimeConversion { reason: String },
17
18    #[error("error converting message type into domain type: {reason}")]
19    TryFromProtobuf { reason: String },
20
21    // Keep messages consistent with previous flex_error display strings
22    #[error("error encoding message into buffer")]
23    EncodeMessage(EncodeError),
24
25    #[error("error decoding buffer into message")]
26    DecodeMessage(DecodeError),
27
28    #[error("error parsing encoded length")]
29    ParseLength(TryFromIntError),
30}
31
32impl Error {
33    // Backwards-compatible constructors mirroring flex_error-generated fns
34    pub fn time_conversion(reason: String) -> Self {
35        Self::TimeConversion { reason }
36    }
37
38    pub fn try_from_protobuf(reason: String) -> Self {
39        Self::TryFromProtobuf { reason }
40    }
41
42    pub fn encode_message(err: EncodeError) -> Self {
43        Self::EncodeMessage(err)
44    }
45
46    pub fn decode_message(err: DecodeError) -> Self {
47        Self::DecodeMessage(err)
48    }
49
50    pub fn parse_length(err: TryFromIntError) -> Self {
51        Self::ParseLength(err)
52    }
53
54    pub fn try_from<Raw, T, E>(e: E) -> Error
55    where
56        E: Display,
57        T: TryFrom<Raw, Error = E>,
58    {
59        Error::try_from_protobuf(format!("{e}"))
60    }
61}