refactor(proto): return Error::Incomplete instead of UnexpectedEof

This commit is contained in:
Sean McArthur
2018-01-11 11:45:28 -08:00
parent 30f7f1dbe6
commit 7888451408
3 changed files with 20 additions and 18 deletions

View File

@@ -58,6 +58,21 @@ where I: AsyncRead + AsyncWrite,
fn poll_incoming(&mut self) -> Poll<Option<Frame<super::MessageHead<T::Incoming>, super::Chunk, ::Error>>, io::Error> {
trace!("Conn::poll_incoming()");
#[derive(Debug)]
struct ParseEof;
impl fmt::Display for ParseEof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(::std::error::Error::description(self))
}
}
impl ::std::error::Error for ParseEof {
fn description(&self) -> &str {
"end of file reached before parsing could complete"
}
}
loop {
if self.is_read_closed() {
trace!("Conn::poll when closed");
@@ -73,6 +88,9 @@ where I: AsyncRead + AsyncWrite,
Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
Ok(Async::NotReady) => Ok(Async::NotReady),
Err(::Error::Io(err)) => Err(err),
Err(::Error::Incomplete) => {
Err(io::Error::new(io::ErrorKind::UnexpectedEof, ParseEof))
},
Err(err) => Ok(Async::Ready(Some(Frame::Error {
error: err,
}))),

View File

@@ -84,8 +84,7 @@ impl<T: AsyncRead + AsyncWrite> Buffered<T> {
match try_ready!(self.read_from_io()) {
0 => {
trace!("parse eof");
//TODO: utilize Error::Incomplete when Error type is redesigned
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, ParseEof).into());
return Err(::Error::Incomplete);
}
_ => {},
}
@@ -333,21 +332,6 @@ impl WriteBuf {
}
}
#[derive(Debug)]
struct ParseEof;
impl fmt::Display for ParseEof {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(::std::error::Error::description(self))
}
}
impl ::std::error::Error for ParseEof {
fn description(&self) -> &str {
"end of file reached before parsing could complete"
}
}
// TODO: Move tests to their own mod
#[cfg(test)]
use std::io::Read;

View File

@@ -425,7 +425,7 @@ test! {
body: None,
proxy: false,
error: |err| match err {
&hyper::Error::Io(_) => true,
&hyper::Error::Incomplete => true,
_ => false,
},
}