From c364e311be5a0a3269283d2ed90f07515b8a5941 Mon Sep 17 00:00:00 2001 From: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com> Date: Sat, 9 Oct 2021 02:11:46 -0400 Subject: [PATCH 1/2] use thiserror to remove error.rs boilerplate --- Cargo.lock | 9 +++++---- rust/protocol/Cargo.toml | 1 + rust/protocol/src/error.rs | 33 +++++---------------------------- 3 files changed, 11 insertions(+), 32 deletions(-) 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..1affac5de 100644 --- a/rust/protocol/src/error.rs +++ b/rust/protocol/src/error.rs @@ -6,13 +6,13 @@ 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), @@ -20,9 +20,9 @@ pub enum SignalProtocolError { InvalidState(&'static str, String), /// 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 +93,7 @@ pub enum SignalProtocolError { /// error in method call '{0}': {1} ApplicationCallbackError( &'static str, - Box, + #[source] Box, ), /// invalid sealed sender message {0} @@ -103,26 +103,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) - } -} From de8d7f1ec7aad1414aa134b76a6328a9dc08aa00 Mon Sep 17 00:00:00 2001 From: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com> Date: Sun, 10 Oct 2021 15:06:17 -0400 Subject: [PATCH 2/2] add note on removing duplicated stack trace information --- rust/protocol/src/error.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rust/protocol/src/error.rs b/rust/protocol/src/error.rs index 1affac5de..ae4232603 100644 --- a/rust/protocol/src/error.rs +++ b/rust/protocol/src/error.rs @@ -19,6 +19,9 @@ pub enum SignalProtocolError { /// 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(#[from] prost::DecodeError), /// failed to encode protobuf: {0}