Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions libp2p-swarm/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -556,7 +556,7 @@ impl<'a, T, C> UpgradedNode<T, C>
let iter = upgrade.protocol_names()
.map(|(name, id)| (name, <Bytes as PartialEq>::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)| {
Expand Down Expand Up @@ -605,14 +605,12 @@ impl<'a, T, C> UpgradedNode<T, C>
(n, <Bytes as PartialEq>::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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: I prefer use std::io; to use std::io::Error as IoError;, but that could be argued as personal taste.

})
.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))
Expand Down
35 changes: 35 additions & 0 deletions multistream-select/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -49,3 +51,36 @@ impl From<IoError> 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: maybe use an if let?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A match is better if we add more variants in the future

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))
}
}
38 changes: 38 additions & 0 deletions multistream-select/src/protocol/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -44,3 +46,39 @@ impl From<IoError> 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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we repeat Error/MultistreamSelect twice in this? Can it be enum Error { Io(...), ... }? We know we're in the multistream select crate already.

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))
}
}