diff --git a/Cargo.lock b/Cargo.lock index 56957a842..701a986bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -868,6 +868,7 @@ dependencies = [ "rand 0.7.3", "sha2", "subtle", + "thiserror", "uuid", "x25519-dalek", ] @@ -1899,18 +1900,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.29" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "602eca064b2d83369e2b2f34b09c70b605402801927c65c11071ac911d299b88" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.29" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bad553cc2c78e8de258400763a647e80e6d1b31ee237275d756f6836d204494c" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" dependencies = [ "proc-macro2", "quote", diff --git a/rust/protocol/Cargo.toml b/rust/protocol/Cargo.toml index 820614a5b..b364931eb 100644 --- a/rust/protocol/Cargo.toml +++ b/rust/protocol/Cargo.toml @@ -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"] diff --git a/rust/protocol/src/error.rs b/rust/protocol/src/error.rs index d71778c56..ae4232603 100644 --- a/rust/protocol/src/error.rs +++ b/rust/protocol/src/error.rs @@ -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 = std::result::Result; -#[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, @@ -93,7 +96,7 @@ pub enum SignalProtocolError { /// error in method call '{0}': {1} ApplicationCallbackError( &'static str, - Box, + #[source] Box, ), /// invalid sealed sender message {0} @@ -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 for SignalProtocolError { - fn from(value: prost::DecodeError) -> SignalProtocolError { - SignalProtocolError::ProtobufDecodingError(value) - } -} - -impl From for SignalProtocolError { - fn from(value: prost::EncodeError) -> SignalProtocolError { - SignalProtocolError::ProtobufEncodingError(value) - } -}