Skip to content

Commit

Permalink
Implement std::error::Error for InboundFailure and OutboundFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Apr 7, 2021
1 parent bb206bc commit b55ef85
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ use std::{
sync::{atomic::AtomicU64, Arc},
task::{Context, Poll}
};
use std::fmt::Formatter;

/// An inbound request or response.
#[derive(Debug)]
Expand Down Expand Up @@ -181,6 +182,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)]
Expand All @@ -201,6 +215,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`].
Expand Down

0 comments on commit b55ef85

Please sign in to comment.