Skip to content

Commit decfead

Browse files
protocols/gossipsub: Implement std::error::Error for error types (#2254)
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
1 parent c13f033 commit decfead

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
- Allow `message_id_fn`s to accept closures that capture variables.
1515
[PR 2103](https://github.com/libp2p/rust-libp2p/pull/2103)
1616

17+
- Implement std::error::Error for error types.
18+
[PR 2254](https://github.com/libp2p/rust-libp2p/pull/2254)
19+
1720
# 0.32.0 [2021-07-12]
1821

1922
- Update dependencies.

protocols/gossipsub/src/error.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ pub enum PublishError {
4040
TransformFailed(std::io::Error),
4141
}
4242

43+
impl std::fmt::Display for PublishError {
44+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
45+
write!(f, "{:?}", self)
46+
}
47+
}
48+
49+
impl std::error::Error for PublishError {
50+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
51+
match self {
52+
Self::SigningError(err) => Some(err),
53+
Self::TransformFailed(err) => Some(err),
54+
_ => None,
55+
}
56+
}
57+
}
58+
4359
/// Error associated with subscribing to a topic.
4460
#[derive(Debug)]
4561
pub enum SubscriptionError {
@@ -49,6 +65,21 @@ pub enum SubscriptionError {
4965
NotAllowed,
5066
}
5167

68+
impl std::fmt::Display for SubscriptionError {
69+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
70+
write!(f, "{:?}", self)
71+
}
72+
}
73+
74+
impl std::error::Error for SubscriptionError {
75+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
76+
match self {
77+
Self::PublishError(err) => Some(err),
78+
_ => None,
79+
}
80+
}
81+
}
82+
5283
impl From<SigningError> for PublishError {
5384
fn from(error: SigningError) -> Self {
5485
PublishError::SigningError(error)
@@ -95,6 +126,14 @@ pub enum ValidationError {
95126
TransformFailed,
96127
}
97128

129+
impl std::fmt::Display for ValidationError {
130+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
131+
write!(f, "{:?}", self)
132+
}
133+
}
134+
135+
impl std::error::Error for ValidationError {}
136+
98137
impl From<std::io::Error> for GossipsubHandlerError {
99138
fn from(error: std::io::Error) -> GossipsubHandlerError {
100139
GossipsubHandlerError::Io(error)

0 commit comments

Comments
 (0)