Skip to content

Commit

Permalink
ProtobufError: replace description with Display
Browse files Browse the repository at this point in the history
Description is deprecated
  • Loading branch information
stepancheg committed Dec 31, 2019
1 parent f64b967 commit 24c20a0
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions protobuf/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ pub enum WireError {
Other,
}

impl fmt::Display for WireError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WireError::Utf8Error => write!(f, "invalid UTF-8 sequence"),
WireError::UnexpectedWireType(..) => write!(f, "unexpected wire type"),
WireError::InvalidEnumValue(..) => write!(f, "invalid enum value"),
WireError::IncorrectTag(..) => write!(f, "incorrect tag"),
WireError::IncorrectVarint => write!(f, "incorrect varint"),
WireError::IncompleteMap => write!(f, "incomplete map"),
WireError::UnexpectedEof => write!(f, "unexpected EOF"),
WireError::OverRecursionLimit => write!(f, "over recursion limit"),
WireError::TruncatedMessage => write!(f, "truncated message"),
WireError::Other => write!(f, "other error"),
}
}
}

/// Generic protobuf error
#[derive(Debug)]
pub enum ProtobufError {
Expand All @@ -40,32 +57,17 @@ pub enum ProtobufError {

impl fmt::Display for ProtobufError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

impl Error for ProtobufError {
fn description(&self) -> &str {
match self {
// not sure that cause should be included in message
&ProtobufError::IoError(ref e) => e.description(),
&ProtobufError::WireError(ref e) => match *e {
WireError::Utf8Error => "invalid UTF-8 sequence",
WireError::UnexpectedWireType(..) => "unexpected wire type",
WireError::InvalidEnumValue(..) => "invalid enum value",
WireError::IncorrectTag(..) => "incorrect tag",
WireError::IncorrectVarint => "incorrect varint",
WireError::IncompleteMap => "incomplete map",
WireError::UnexpectedEof => "unexpected EOF",
WireError::OverRecursionLimit => "over recursion limit",
WireError::TruncatedMessage => "truncated message",
WireError::Other => "other error",
},
&ProtobufError::Utf8(ref e) => &e.description(),
&ProtobufError::MessageNotInitialized { .. } => "not all message fields set",
&ProtobufError::IoError(ref e) => write!(f, "IO error: {}", e),
&ProtobufError::WireError(ref e) => fmt::Display::fmt(e, f),
&ProtobufError::Utf8(ref e) => write!(f, "{}", e),
&ProtobufError::MessageNotInitialized { .. } => write!(f, "not all message fields set"),
}
}
}

impl Error for ProtobufError {
fn cause(&self) -> Option<&dyn Error> {
match self {
&ProtobufError::IoError(ref e) => Some(e),
Expand Down

0 comments on commit 24c20a0

Please sign in to comment.