diff --git a/proto/Cargo.toml b/proto/Cargo.toml index a4b5bd7d..56f64958 100644 --- a/proto/Cargo.toml +++ b/proto/Cargo.toml @@ -30,7 +30,7 @@ time = { version = "0.3", default-features = false, features = [ "macros", "parsing", ] } -flex-error = { version = "0.4.4", default-features = false } +thiserror = { version = "1.0.40" } chrono = { version = "0.4.24", default-features = false } derive_more = { version = "0.99.17" } diff --git a/proto/src/error.rs b/proto/src/error.rs index 1495ff2d..6d2bdfd5 100644 --- a/proto/src/error.rs +++ b/proto/src/error.rs @@ -1,34 +1,30 @@ //! This module defines the various errors that be raised during Protobuf //! conversions. - +extern crate std; use core::{convert::TryFrom, fmt::Display, num::TryFromIntError}; -use flex_error::{define_error, DisplayOnly}; use prost::{DecodeError, EncodeError}; use crate::prelude::*; -define_error! { - Error { - TryFromProtobuf - { reason: String } - | e | { - format!("error converting message type into domain type: {}", - e.reason) - }, - - EncodeMessage - [ DisplayOnly ] - | _ | { "error encoding message into buffer" }, - - DecodeMessage - [ DisplayOnly ] - | _ | { "error decoding buffer into message" }, - - ParseLength - [ DisplayOnly ] - | _ | { "error parsing encoded length" }, - } +#[derive(thiserror::Error, Debug)] +pub enum Error { + /// error converting message type into domain type + #[error("error converting message type into domain type: {0}")] + TryFromProtobuf(String), + + /// error encoding message into buffer + #[error("error encoding message into buffer: {0:?}")] + EncodeMessage(EncodeError), + /// error decoding buffer into message + #[error("error decoding buffer into message: {0:?}")] + DecodeMessage(DecodeError), + /// error decoding buffer into message + #[error("error building canonical message: {0}")] + CreateCanonical(String), + /// error parsing encoded length + #[error("error parsing encoded length: {0:?}")] + ParseLength(TryFromIntError), } impl Error { @@ -37,6 +33,6 @@ impl Error { E: Display, T: TryFrom, { - Error::try_from_protobuf(format!("{e}")) + Error::TryFromProtobuf(format!("{e}")) } } diff --git a/proto/src/lib.rs b/proto/src/lib.rs index e5c82657..1f43a9ab 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -130,7 +130,7 @@ where fn encode(&self, buf: &mut B) -> Result<(), Error> { T::from(self.clone()) .encode(buf) - .map_err(Error::encode_message) + .map_err(Error::EncodeMessage) } /// Encode with a length-delimiter to a buffer in Protobuf format. @@ -145,7 +145,7 @@ where fn encode_length_delimited(&self, buf: &mut B) -> Result<(), Error> { T::from(self.clone()) .encode_length_delimited(buf) - .map_err(Error::encode_message) + .map_err(Error::EncodeMessage) } /// Constructor that attempts to decode an instance from a buffer. @@ -157,7 +157,7 @@ where /// /// [`prost::Message::decode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode fn decode(buf: B) -> Result { - let raw = T::decode(buf).map_err(Error::decode_message)?; + let raw = T::decode(buf).map_err(Error::DecodeMessage)?; Self::try_from(raw).map_err(Error::try_from::) } @@ -172,7 +172,7 @@ where /// /// [`prost::Message::decode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode_length_delimited fn decode_length_delimited(buf: B) -> Result { - let raw = T::decode_length_delimited(buf).map_err(Error::decode_message)?; + let raw = T::decode_length_delimited(buf).map_err(Error::DecodeMessage)?; Self::try_from(raw).map_err(Error::try_from::) } @@ -202,7 +202,7 @@ where /// Encode with a length-delimiter to a `Vec` Protobuf-encoded message. fn encode_length_delimited_vec(&self) -> Result, Error> { let len = self.encoded_len(); - let lenu64 = len.try_into().map_err(Error::parse_length)?; + let lenu64 = len.try_into().map_err(Error::ParseLength)?; let mut wire = Vec::with_capacity(len + encoded_len_varint(lenu64)); self.encode_length_delimited(&mut wire).map(|_| wire) }