Skip to content

Commit ff2fe98

Browse files
committed
Fix error handling in Swarm dial() and listen_on()
1 parent 4a767e0 commit ff2fe98

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

libp2p-swarm/src/transport.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use futures::{Stream, Poll, Async};
3434
use futures::future::{IntoFuture, Future, ok as future_ok, FutureResult};
3535
use multiaddr::Multiaddr;
3636
use multistream_select;
37-
use std::io::{Cursor, Error as IoError, Read, Write};
37+
use std::io::{Cursor, Error as IoError, ErrorKind as IoErrorKind, Read, Write};
3838
use std::iter;
3939
use tokio_io::{AsyncRead, AsyncWrite};
4040

@@ -530,7 +530,7 @@ impl<'a, T, C> UpgradedNode<T, C>
530530
let iter = upgrade.protocol_names()
531531
.map(|(name, id)| (name, <Bytes as PartialEq>::eq, id));
532532
let negotiated = multistream_select::dialer_select_proto(connection, iter)
533-
.map_err(|err| panic!("{:?}", err)); // TODO:
533+
.map_err(|err| IoError::new(IoErrorKind::Other, err));
534534
negotiated.map(|(upgrade_id, conn)| (upgrade_id, conn, upgrade))
535535
})
536536
.and_then(|(upgrade_id, connection, upgrade)| {
@@ -579,14 +579,12 @@ impl<'a, T, C> UpgradedNode<T, C>
579579
(n, <Bytes as PartialEq>::eq, t)
580580
}
581581
let iter = upgrade.protocol_names().map(iter_map);
582-
let negotiated = multistream_select::listener_select_proto(connection, iter)
583-
.map_err(|err| panic!("{:?}", err)); // TODO:
582+
let negotiated = multistream_select::listener_select_proto(connection, iter);
584583
negotiated.map(move |(upgrade_id, conn)| (upgrade_id, conn, upgrade, client_addr))
585-
.map_err(|_| panic!()) // TODO:
584+
.map_err(|err| IoError::new(IoErrorKind::Other, err))
586585
})
587-
.map_err(|err| panic!("{:?}", err)) // TODO:
588586
.and_then(|(upgrade_id, connection, upgrade, client_addr)| {
589-
upgrade.upgrade(connection, upgrade_id).map(|c| (c, client_addr))
587+
upgrade.upgrade(connection, upgrade_id).map(|s| (s, client_addr))
590588
});
591589

592590
Ok((Box::new(stream), new_addr))

multistream-select/src/error.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
//! Main `ProtocolChoiceError` error.
2222
2323
use protocol::MultistreamSelectError;
24+
use std::error;
25+
use std::fmt;
2426
use std::io::Error as IoError;
2527

2628
/// Error that can happen when negotiating a protocol with the remote.
@@ -49,3 +51,36 @@ impl From<IoError> for ProtocolChoiceError {
4951
MultistreamSelectError::from(err).into()
5052
}
5153
}
54+
55+
impl error::Error for ProtocolChoiceError {
56+
#[inline]
57+
fn description(&self) -> &str {
58+
match *self {
59+
ProtocolChoiceError::MultistreamSelectError(_) => {
60+
"error in the protocol"
61+
},
62+
ProtocolChoiceError::UnexpectedMessage => {
63+
"received a message from the remote that makes no sense in the current context"
64+
},
65+
ProtocolChoiceError::NoProtocolFound => {
66+
"we don't support any protocol in common with the remote"
67+
},
68+
}
69+
}
70+
71+
fn cause(&self) -> Option<&error::Error> {
72+
match *self {
73+
ProtocolChoiceError::MultistreamSelectError(ref err) => {
74+
Some(err)
75+
}
76+
_ => None,
77+
}
78+
}
79+
}
80+
81+
impl fmt::Display for ProtocolChoiceError {
82+
#[inline]
83+
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
84+
write!(fmt, "{}", error::Error::description(self))
85+
}
86+
}

multistream-select/src/protocol/error.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
//! Contains the error structs for the low-level protocol handling.
2222
23+
use std::error;
24+
use std::fmt;
2325
use std::io::Error as IoError;
2426

2527
/// Error at the multistream-select layer of communication.
@@ -44,3 +46,39 @@ impl From<IoError> for MultistreamSelectError {
4446
MultistreamSelectError::IoError(err)
4547
}
4648
}
49+
50+
impl error::Error for MultistreamSelectError {
51+
#[inline]
52+
fn description(&self) -> &str {
53+
match *self {
54+
MultistreamSelectError::IoError(_) => {
55+
"I/O error"
56+
},
57+
MultistreamSelectError::FailedHandshake => {
58+
"the remote doesn't use the same multistream-select protocol as we do"
59+
},
60+
MultistreamSelectError::UnknownMessage => {
61+
"received an unknown message from the remote"
62+
},
63+
MultistreamSelectError::WrongProtocolName => {
64+
"protocol names must always start with `/`, otherwise this error is returned"
65+
},
66+
}
67+
}
68+
69+
fn cause(&self) -> Option<&error::Error> {
70+
match *self {
71+
MultistreamSelectError::IoError(ref err) => {
72+
Some(err)
73+
}
74+
_ => None,
75+
}
76+
}
77+
}
78+
79+
impl fmt::Display for MultistreamSelectError {
80+
#[inline]
81+
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
82+
write!(fmt, "{}", error::Error::description(self))
83+
}
84+
}

0 commit comments

Comments
 (0)