diff --git a/libp2p-swarm/src/transport.rs b/libp2p-swarm/src/transport.rs index 8bf8cf48f23..b38380018fb 100644 --- a/libp2p-swarm/src/transport.rs +++ b/libp2p-swarm/src/transport.rs @@ -35,7 +35,7 @@ use futures::future::{IntoFuture, Future, ok as future_ok, FutureResult}; use multiaddr::Multiaddr; use multistream_select; use muxing::StreamMuxer; -use std::io::{Cursor, Error as IoError, Read, Write}; +use std::io::{Cursor, Error as IoError, ErrorKind as IoErrorKind, Read, Write}; use std::iter; use tokio_io::{AsyncRead, AsyncWrite}; @@ -556,7 +556,7 @@ impl<'a, T, C> UpgradedNode let iter = upgrade.protocol_names() .map(|(name, id)| (name, ::eq, id)); let negotiated = multistream_select::dialer_select_proto(connection, iter) - .map_err(|err| panic!("{:?}", err)); // TODO: + .map_err(|err| IoError::new(IoErrorKind::Other, err)); negotiated.map(|(upgrade_id, conn)| (upgrade_id, conn, upgrade)) }) .and_then(|(upgrade_id, connection, upgrade)| { @@ -605,14 +605,12 @@ impl<'a, T, C> UpgradedNode (n, ::eq, t) } let iter = upgrade.protocol_names().map(iter_map); - let negotiated = multistream_select::listener_select_proto(connection, iter) - .map_err(|err| panic!("{:?}", err)); // TODO: + let negotiated = multistream_select::listener_select_proto(connection, iter); negotiated.map(move |(upgrade_id, conn)| (upgrade_id, conn, upgrade, client_addr)) - .map_err(|_| panic!()) // TODO: + .map_err(|err| IoError::new(IoErrorKind::Other, err)) }) - .map_err(|err| panic!("{:?}", err)) // TODO: .and_then(|(upgrade_id, connection, upgrade, client_addr)| { - upgrade.upgrade(connection, upgrade_id).map(|c| (c, client_addr)) + upgrade.upgrade(connection, upgrade_id).map(|s| (s, client_addr)) }); Ok((Box::new(stream), new_addr)) diff --git a/multistream-select/src/error.rs b/multistream-select/src/error.rs index 84a26e097fe..8f8d5fa271c 100644 --- a/multistream-select/src/error.rs +++ b/multistream-select/src/error.rs @@ -21,6 +21,8 @@ //! Main `ProtocolChoiceError` error. use protocol::MultistreamSelectError; +use std::error; +use std::fmt; use std::io::Error as IoError; /// Error that can happen when negotiating a protocol with the remote. @@ -49,3 +51,36 @@ impl From for ProtocolChoiceError { MultistreamSelectError::from(err).into() } } + +impl error::Error for ProtocolChoiceError { + #[inline] + fn description(&self) -> &str { + match *self { + ProtocolChoiceError::MultistreamSelectError(_) => { + "error in the protocol" + }, + ProtocolChoiceError::UnexpectedMessage => { + "received a message from the remote that makes no sense in the current context" + }, + ProtocolChoiceError::NoProtocolFound => { + "we don't support any protocol in common with the remote" + }, + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + ProtocolChoiceError::MultistreamSelectError(ref err) => { + Some(err) + } + _ => None, + } + } +} + +impl fmt::Display for ProtocolChoiceError { + #[inline] + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(fmt, "{}", error::Error::description(self)) + } +} diff --git a/multistream-select/src/protocol/error.rs b/multistream-select/src/protocol/error.rs index 1c0ff25c249..ceaebab7331 100644 --- a/multistream-select/src/protocol/error.rs +++ b/multistream-select/src/protocol/error.rs @@ -20,6 +20,8 @@ //! Contains the error structs for the low-level protocol handling. +use std::error; +use std::fmt; use std::io::Error as IoError; /// Error at the multistream-select layer of communication. @@ -44,3 +46,39 @@ impl From for MultistreamSelectError { MultistreamSelectError::IoError(err) } } + +impl error::Error for MultistreamSelectError { + #[inline] + fn description(&self) -> &str { + match *self { + MultistreamSelectError::IoError(_) => { + "I/O error" + }, + MultistreamSelectError::FailedHandshake => { + "the remote doesn't use the same multistream-select protocol as we do" + }, + MultistreamSelectError::UnknownMessage => { + "received an unknown message from the remote" + }, + MultistreamSelectError::WrongProtocolName => { + "protocol names must always start with `/`, otherwise this error is returned" + }, + } + } + + fn cause(&self) -> Option<&error::Error> { + match *self { + MultistreamSelectError::IoError(ref err) => { + Some(err) + } + _ => None, + } + } +} + +impl fmt::Display for MultistreamSelectError { + #[inline] + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(fmt, "{}", error::Error::description(self)) + } +}