Skip to content

Commit 0a76d24

Browse files
convert the Display impl to use displaydoc
- remove thiserror for now until we can derive UnwindSafe
1 parent 82f39ff commit 0a76d24

File tree

3 files changed

+54
-111
lines changed

3 files changed

+54
-111
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/protocol/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ hex = "0.4"
2929
log = "0.4"
3030
num_enum = "0.5.1"
3131
uuid = "0.8"
32+
displaydoc = "0.2"
3233

3334
[features]
3435
armv8 = ["aes/armv8", "aes-gcm-siv/armv8"]

rust/protocol/src/error.rs

Lines changed: 41 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,106 @@
11
//
2-
// Copyright 2020 Signal Messenger, LLC.
2+
// Copyright 2020-2021 Signal Messenger, LLC.
33
// SPDX-License-Identifier: AGPL-3.0-only
44
//
55

66
use crate::curve::KeyType;
77

8+
use displaydoc::Display;
9+
810
use std::error::Error;
9-
use std::fmt;
1011
use std::panic::UnwindSafe;
1112

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

14-
#[derive(Debug)]
15+
#[derive(Debug, Display)]
1516
pub enum SignalProtocolError {
17+
/// invalid argument: {0}
1618
InvalidArgument(String),
19+
/// invalid state for call to {0} to succeed: {1}
1720
InvalidState(&'static str, String),
1821

22+
/// failed to decode protobuf: {0}
1923
ProtobufDecodingError(prost::DecodeError),
24+
/// failed to encode protobuf: {0}
2025
ProtobufEncodingError(prost::EncodeError),
26+
/// protobuf encoding was invalid
2127
InvalidProtobufEncoding,
2228

29+
/// ciphertext serialized bytes were too short <{0}>
2330
CiphertextMessageTooShort(usize),
31+
/// ciphertext version was too old <{0}>
2432
LegacyCiphertextVersion(u8),
33+
/// ciphertext version was unrecognized <{0}>
2534
UnrecognizedCiphertextVersion(u8),
35+
/// unrecognized message version <{0}>
2636
UnrecognizedMessageVersion(u32),
2737

38+
/// fingerprint identifiers do not match
2839
FingerprintIdentifierMismatch,
40+
/// fingerprint version number mismatch them {0} us {1}
2941
FingerprintVersionMismatch(u32, u32),
42+
/// fingerprint parsing error
3043
FingerprintParsingError,
3144

45+
/// no key type identifier
3246
NoKeyTypeIdentifier,
47+
/// bad key type <{0:#04x}>
3348
BadKeyType(u8),
49+
/// bad key length <{1}> for key with type <{0}>
3450
BadKeyLength(KeyType, usize),
3551

52+
/// invalid signature detected
3653
SignatureValidationFailed,
3754

55+
/// untrusted identity for address {0}
3856
UntrustedIdentity(crate::ProtocolAddress),
3957

58+
/// invalid prekey identifier
4059
InvalidPreKeyId,
60+
/// invalid signed prekey identifier
4161
InvalidSignedPreKeyId,
4262

63+
/// invalid root key length <{0}>
4364
InvalidRootKeyLength(usize),
65+
/// invalid chain key length <{0}>
4466
InvalidChainKeyLength(usize),
4567

68+
/// invalid MAC key length <{0}>
4669
InvalidMacKeyLength(usize),
70+
/// invalid cipher key length <{0}> or nonce length <{1}>
4771
InvalidCipherCryptographicParameters(usize, usize),
72+
/// invalid ciphertext message
4873
InvalidCiphertext,
4974

75+
/// no sender key state
5076
NoSenderKeyState,
5177

78+
/// session with '{0}' not found
5279
SessionNotFound(String),
80+
/// invalid session structure
5381
InvalidSessionStructure,
82+
/// session for {0} has invalid registration ID {1:X}
5483
InvalidRegistrationId(crate::ProtocolAddress, u32),
5584

85+
/// message with old counter {0} / {1}
5686
DuplicatedMessage(u32, u32),
87+
/// invalid message {0}
5788
InvalidMessage(&'static str),
89+
/// internal error {0}
5890
InternalError(&'static str),
91+
/// error while invoking an ffi callback: {0}
5992
FfiBindingError(String),
93+
/// error in method call '{0}': {1}
6094
ApplicationCallbackError(
6195
&'static str,
62-
Box<dyn Error + Send + Sync + UnwindSafe + 'static>,
96+
Box<dyn std::error::Error + Send + Sync + UnwindSafe + 'static>,
6397
),
6498

99+
/// invalid sealed sender message {0}
65100
InvalidSealedSenderMessage(String),
101+
/// unknown sealed sender message version {0}
66102
UnknownSealedSenderVersion(u8),
103+
/// self send of a sealed sender message
67104
SealedSenderSelfSend,
68105
}
69106

@@ -89,110 +126,3 @@ impl From<prost::EncodeError> for SignalProtocolError {
89126
SignalProtocolError::ProtobufEncodingError(value)
90127
}
91128
}
92-
93-
impl fmt::Display for SignalProtocolError {
94-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95-
match self {
96-
SignalProtocolError::ProtobufDecodingError(e) => {
97-
write!(f, "failed to decode protobuf: {}", e)
98-
}
99-
SignalProtocolError::ProtobufEncodingError(e) => {
100-
write!(f, "failed to encode protobuf: {}", e)
101-
}
102-
SignalProtocolError::InvalidProtobufEncoding => {
103-
write!(f, "protobuf encoding was invalid")
104-
}
105-
SignalProtocolError::InvalidArgument(s) => write!(f, "invalid argument: {}", s),
106-
SignalProtocolError::InvalidState(func, s) => {
107-
write!(f, "invalid state for call to {} to succeed: {}", func, s)
108-
}
109-
SignalProtocolError::CiphertextMessageTooShort(size) => {
110-
write!(f, "ciphertext serialized bytes were too short <{}>", size)
111-
}
112-
SignalProtocolError::LegacyCiphertextVersion(version) => {
113-
write!(f, "ciphertext version was too old <{}>", version)
114-
}
115-
SignalProtocolError::UnrecognizedCiphertextVersion(version) => {
116-
write!(f, "ciphertext version was unrecognized <{}>", version)
117-
}
118-
SignalProtocolError::UnrecognizedMessageVersion(message_version) => {
119-
write!(f, "unrecognized message version <{}>", message_version)
120-
}
121-
SignalProtocolError::FingerprintIdentifierMismatch => {
122-
write!(f, "fingerprint identifiers do not match")
123-
}
124-
SignalProtocolError::FingerprintVersionMismatch(theirs, ours) => {
125-
write!(
126-
f,
127-
"fingerprint version number mismatch them {} us {}",
128-
theirs, ours
129-
)
130-
}
131-
SignalProtocolError::FingerprintParsingError => {
132-
write!(f, "fingerprint parsing error")
133-
}
134-
SignalProtocolError::NoKeyTypeIdentifier => write!(f, "no key type identifier"),
135-
SignalProtocolError::BadKeyType(t) => write!(f, "bad key type <{:#04x}>", t),
136-
SignalProtocolError::BadKeyLength(t, l) => {
137-
write!(f, "bad key length <{}> for key with type <{}>", l, t)
138-
}
139-
SignalProtocolError::InvalidPreKeyId => write!(f, "invalid prekey identifier"),
140-
SignalProtocolError::InvalidSignedPreKeyId => {
141-
write!(f, "invalid signed prekey identifier")
142-
}
143-
SignalProtocolError::InvalidChainKeyLength(l) => {
144-
write!(f, "invalid chain key length <{}>", l)
145-
}
146-
SignalProtocolError::InvalidRootKeyLength(l) => {
147-
write!(f, "invalid root key length <{}>", l)
148-
}
149-
SignalProtocolError::InvalidCipherCryptographicParameters(kl, nl) => write!(
150-
f,
151-
"invalid cipher key length <{}> or nonce length <{}>",
152-
kl, nl
153-
),
154-
SignalProtocolError::InvalidMacKeyLength(l) => {
155-
write!(f, "invalid MAC key length <{}>", l)
156-
}
157-
SignalProtocolError::UntrustedIdentity(addr) => {
158-
write!(f, "untrusted identity for address {}", addr)
159-
}
160-
SignalProtocolError::SignatureValidationFailed => {
161-
write!(f, "invalid signature detected")
162-
}
163-
SignalProtocolError::InvalidCiphertext => write!(f, "invalid ciphertext message"),
164-
SignalProtocolError::SessionNotFound(who) => {
165-
write!(f, "session with '{}' not found", who)
166-
}
167-
SignalProtocolError::InvalidSessionStructure => write!(f, "invalid session structure"),
168-
SignalProtocolError::InvalidRegistrationId(addr, value) => {
169-
write!(
170-
f,
171-
"session for {} has invalid registration ID {:X}",
172-
addr, value
173-
)
174-
}
175-
SignalProtocolError::DuplicatedMessage(i, c) => {
176-
write!(f, "message with old counter {} / {}", i, c)
177-
}
178-
SignalProtocolError::InvalidMessage(m) => write!(f, "invalid message {}", m),
179-
SignalProtocolError::InternalError(m) => write!(f, "internal error {}", m),
180-
SignalProtocolError::NoSenderKeyState => write!(f, "no sender key state"),
181-
SignalProtocolError::FfiBindingError(m) => {
182-
write!(f, "error while invoking an ffi callback: {}", m)
183-
}
184-
SignalProtocolError::ApplicationCallbackError(func, c) => {
185-
write!(f, "application callback {} failed with {}", func, c)
186-
}
187-
SignalProtocolError::InvalidSealedSenderMessage(m) => {
188-
write!(f, "invalid sealed sender message {}", m)
189-
}
190-
SignalProtocolError::UnknownSealedSenderVersion(v) => {
191-
write!(f, "unknown sealed sender message version {}", v)
192-
}
193-
SignalProtocolError::SealedSenderSelfSend => {
194-
write!(f, "self send of a sealed sender message")
195-
}
196-
}
197-
}
198-
}

0 commit comments

Comments
 (0)