From 2b8ff0bdb01bdd03f9de352eb45ae1e6c26f56c2 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 7 Apr 2021 14:19:26 +1000 Subject: [PATCH 1/3] Implement std::error::Error for InboundFailure and OutboundFailure Fixes #2017. --- protocols/request-response/src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index dae0c11cba1..14fe2cdd1d5 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -181,6 +181,19 @@ pub enum OutboundFailure { UnsupportedProtocols, } +impl fmt::Display for OutboundFailure { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + OutboundFailure::DialFailure => write!(f, "failed to dial the requested peer"), + OutboundFailure::Timeout => write!(f, "timeout while waiting for a response"), + OutboundFailure::ConnectionClosed => write!(f, "connection was closed before a response was received"), + OutboundFailure::UnsupportedProtocols => write!(f, "the remote supports none of the requested protocols") + } + } +} + +impl std::error::Error for OutboundFailure {} + /// Possible failures occurring in the context of receiving an /// inbound request and sending a response. #[derive(Debug, Clone, PartialEq)] @@ -201,6 +214,19 @@ pub enum InboundFailure { ResponseOmission, } +impl fmt::Display for InboundFailure { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + InboundFailure::Timeout => write!(f, "timeout while receiving request or sending response"), + InboundFailure::ConnectionClosed => write!(f, "connection was closed before a response could be sent"), + InboundFailure::UnsupportedProtocols => write!(f, "the local peer supports none of the protocols requested by the remote"), + InboundFailure::ResponseOmission => write!(f, "the response channel was dropped without sending a response to the remote") + } + } +} + +impl std::error::Error for InboundFailure {} + /// A channel for sending a response to an inbound request. /// /// See [`RequestResponse::send_response`]. From 0c7f8843e5d835a8b84523af556a692bc02aff5c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 8 Apr 2021 11:24:54 +1000 Subject: [PATCH 2/3] Add changelog entry --- protocols/request-response/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protocols/request-response/CHANGELOG.md b/protocols/request-response/CHANGELOG.md index c5e83706213..bbb434316c6 100644 --- a/protocols/request-response/CHANGELOG.md +++ b/protocols/request-response/CHANGELOG.md @@ -1,6 +1,8 @@ # 0.11.0 [unreleased] - Update `libp2p-swarm`. +- Implement `std::error::Error` for `InboundFailure` and `OutboundFailure` [PR + 2033](https://github.com/libp2p/rust-libp2p/pull/2033). # 0.10.0 [2021-03-17] From 906704d046f4aee45d7dc37670f30579c7f322d2 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 8 Apr 2021 11:25:09 +1000 Subject: [PATCH 3/3] Make all error messages start with an uppercase letter for consistency --- protocols/request-response/src/lib.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 14fe2cdd1d5..7e5fd58c5c1 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -184,10 +184,10 @@ pub enum OutboundFailure { impl fmt::Display for OutboundFailure { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - OutboundFailure::DialFailure => write!(f, "failed to dial the requested peer"), - OutboundFailure::Timeout => write!(f, "timeout while waiting for a response"), - OutboundFailure::ConnectionClosed => write!(f, "connection was closed before a response was received"), - OutboundFailure::UnsupportedProtocols => write!(f, "the remote supports none of the requested protocols") + OutboundFailure::DialFailure => write!(f, "Failed to dial the requested peer"), + OutboundFailure::Timeout => write!(f, "Timeout while waiting for a response"), + OutboundFailure::ConnectionClosed => write!(f, "Connection was closed before a response was received"), + OutboundFailure::UnsupportedProtocols => write!(f, "The remote supports none of the requested protocols") } } } @@ -217,10 +217,10 @@ pub enum InboundFailure { impl fmt::Display for InboundFailure { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - InboundFailure::Timeout => write!(f, "timeout while receiving request or sending response"), - InboundFailure::ConnectionClosed => write!(f, "connection was closed before a response could be sent"), - InboundFailure::UnsupportedProtocols => write!(f, "the local peer supports none of the protocols requested by the remote"), - InboundFailure::ResponseOmission => write!(f, "the response channel was dropped without sending a response to the remote") + InboundFailure::Timeout => write!(f, "Timeout while receiving request or sending response"), + InboundFailure::ConnectionClosed => write!(f, "Connection was closed before a response could be sent"), + InboundFailure::UnsupportedProtocols => write!(f, "The local peer supports none of the protocols requested by the remote"), + InboundFailure::ResponseOmission => write!(f, "The response channel was dropped without sending a response to the remote") } } }