From 3cfcab016e17f36884ee77ff85947402535468ee Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 7 Oct 2019 15:29:38 -0700 Subject: [PATCH] Remove public Error constructor from io::Error (#420) --- src/client.rs | 2 +- src/error.rs | 16 +++++++--------- src/server.rs | 14 +++++++------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/client.rs b/src/client.rs index 593f02f..e5c535e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1136,7 +1136,7 @@ where log::debug!("binding client connection"); let msg: &'static [u8] = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; - io.write_all(msg).await?; + io.write_all(msg).await.map_err(crate::Error::from_io)?; log::debug!("client connection bound"); diff --git a/src/error.rs b/src/error.rs index 25e1517..c1a13e7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -73,6 +73,12 @@ impl Error { _ => None, } } + + pub(crate) fn from_io(err: io::Error) -> Self { + Error { + kind: Kind::Io(err), + } + } } impl From for Error { @@ -88,14 +94,6 @@ impl From for Error { } } -impl From for Error { - fn from(src: io::Error) -> Error { - Error { - kind: Kind::Io(src), - } - } -} - impl From for Error { fn from(src: Reason) -> Error { Error { @@ -109,7 +107,7 @@ impl From for Error { match src { SendError::User(e) => e.into(), SendError::Connection(reason) => reason.into(), - SendError::Io(e) => e.into(), + SendError::Io(e) => Error::from_io(e), } } } diff --git a/src/server.rs b/src/server.rs index 0e2a3a6..e68de6e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1121,7 +1121,7 @@ where fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { // Flush the codec - ready!(self.codec.as_mut().unwrap().flush(cx))?; + ready!(self.codec.as_mut().unwrap().flush(cx)).map_err(crate::Error::from_io)?; // Return the codec Poll::Ready(Ok(self.codec.take().unwrap())) @@ -1153,13 +1153,13 @@ where let mut rem = PREFACE.len() - self.pos; while rem > 0 { - let n = ready!(Pin::new(self.inner_mut()).poll_read(cx, &mut buf[..rem]))?; + let n = ready!(Pin::new(self.inner_mut()).poll_read(cx, &mut buf[..rem])) + .map_err(crate::Error::from_io)?; if n == 0 { - return Poll::Ready(Err(io::Error::new( - io::ErrorKind::ConnectionReset, - "connection closed unexpectedly", - ) - .into())); + return Poll::Ready(Err(crate::Error::from_io(io::Error::new( + io::ErrorKind::UnexpectedEof, + "connection closed before reading preface", + )))); } if PREFACE[self.pos..self.pos + n] != buf[..n] {