-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix error handling in Swarm dial() and listen_on() #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nit: maybe use an
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<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) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we repeat |
||
| 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)) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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;touse std::io::Error as IoError;, but that could be argued as personal taste.