diff --git a/src/client.rs b/src/client.rs index 0e5faf03..81cc22f5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -407,17 +407,26 @@ impl Client { let mut found_tag_line = false; let start_str = format!("{}{} ", TAG_PREFIX, self.tag); let mut lines: Vec = Vec::new(); + let mut error = None; while !found_tag_line { let raw_data = try!(self.readline()); - let line = String::from_utf8(raw_data).unwrap(); - lines.push(line.clone()); - if (&*line).starts_with(&*start_str) { - found_tag_line = true; + // if there is an encoding error, still continue until the tag + match String::from_utf8(raw_data) { + Err(e) => error = Some(e), + Ok(line) => { + lines.push(line.clone()); + if (&*line).starts_with(&*start_str) { + found_tag_line = true; + } + } } } - Ok(lines) + match error { + Some(e) => Err(e.into()), + None => Ok(lines) + } } fn read_greeting(&mut self) -> Result<()> { diff --git a/src/error.rs b/src/error.rs index 28ee761b..b6b8fa4d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ use std::result; use std::fmt; use std::error::Error as StdError; use std::net::TcpStream; +use std::string::FromUtf8Error; use openssl::ssl::HandshakeError as SslError; @@ -35,6 +36,12 @@ impl From> for Error { } } +impl From for Error { + fn from(err: FromUtf8Error) -> Error { + Error::Parse(ParseError::FromUtf8(err)) + } +} + impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -67,6 +74,8 @@ impl StdError for Error { #[derive(Debug)] pub enum ParseError { + // Error in the decoding of data. + FromUtf8(FromUtf8Error), // Indicates an error parsing the status response. Such as OK, NO, and BAD. StatusResponse(Vec), // Error parsing the cabability response. @@ -86,6 +95,7 @@ impl fmt::Display for ParseError { impl StdError for ParseError { fn description(&self) -> &str { match *self { + ParseError::FromUtf8(_) => "Unable to decode the response as UTF-8.", ParseError::StatusResponse(_) => "Unable to parse status response", ParseError::Capability(_) => "Unable to parse capability response", ParseError::Authentication(_) => "Unable to parse authentication response"