Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use thiserror to remove error.rs boilerplate #376

Merged
merged 2 commits into from Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -30,6 +30,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)
}
}