Skip to content

Commit

Permalink
Merge pull request #376 from cosmicexplorer/thiserror-attempt-2
Browse files Browse the repository at this point in the history
use thiserror to remove error.rs boilerplate
  • Loading branch information
jrose-signal committed Oct 15, 2021
2 parents a71a251 + de8d7f1 commit c7c1abb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/protocol/Cargo.toml
Expand Up @@ -31,6 +31,7 @@ log = "0.4"
num_enum = "0.5.1"
uuid = "0.8"
displaydoc = "0.2"
thiserror = "1.0.30"

[features]
armv8 = ["aes/armv8", "aes-gcm-siv/armv8"]
Expand Down
36 changes: 8 additions & 28 deletions rust/protocol/src/error.rs
Expand Up @@ -6,23 +6,26 @@
use crate::curve::KeyType;

use displaydoc::Display;
use thiserror::Error;

use std::error::Error;
use std::panic::UnwindSafe;

pub type Result<T> = std::result::Result<T, SignalProtocolError>;

#[derive(Debug, Display)]
#[derive(Debug, Display, Error)]
pub enum SignalProtocolError {
/// invalid argument: {0}
InvalidArgument(String),
/// invalid state for call to {0} to succeed: {1}
InvalidState(&'static str, String),

// TODO: avoid duplicating error information in the Display impl and the #[from] or #[source]
// attribute if/when we switch to using an error reporting mechanism supporting stack traces:
// see https://github.com/yaahc/blog.rust-lang.org/blob/master/posts/inside-rust/2021-05-15-What-the-error-handling-project-group-is-working-towards.md#duplicate-information-issue
/// failed to decode protobuf: {0}
ProtobufDecodingError(prost::DecodeError),
ProtobufDecodingError(#[from] prost::DecodeError),
/// failed to encode protobuf: {0}
ProtobufEncodingError(prost::EncodeError),
ProtobufEncodingError(#[from] prost::EncodeError),
/// protobuf encoding was invalid
InvalidProtobufEncoding,

Expand Down Expand Up @@ -93,7 +96,7 @@ pub enum SignalProtocolError {
/// error in method call '{0}': {1}
ApplicationCallbackError(
&'static str,
Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
#[source] Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
),

/// invalid sealed sender message {0}
Expand All @@ -103,26 +106,3 @@ pub enum SignalProtocolError {
/// self send of a sealed sender message
SealedSenderSelfSend,
}

impl Error for SignalProtocolError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
SignalProtocolError::ProtobufEncodingError(e) => Some(e),
SignalProtocolError::ProtobufDecodingError(e) => Some(e),
SignalProtocolError::ApplicationCallbackError(_, e) => Some(e.as_ref()),
_ => None,
}
}
}

impl From<prost::DecodeError> for SignalProtocolError {
fn from(value: prost::DecodeError) -> SignalProtocolError {
SignalProtocolError::ProtobufDecodingError(value)
}
}

impl From<prost::EncodeError> for SignalProtocolError {
fn from(value: prost::EncodeError) -> SignalProtocolError {
SignalProtocolError::ProtobufEncodingError(value)
}
}

0 comments on commit c7c1abb

Please sign in to comment.