Skip to content

Commit defc3c2

Browse files
Sander MaijersSander Maijers
Sander Maijers
authored and
Sander Maijers
committed
Don’t panic on receiving data not encoded in UTF-8
Return a `Result` instead.
1 parent 0a0bf6e commit defc3c2

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::mailbox::Mailbox;
88
use super::authenticator::Authenticator;
99
use super::parse::{parse_authenticate_response, parse_capability, parse_response,
1010
parse_response_ok, parse_select_or_examine};
11-
use super::error::{Error, Result};
11+
use super::error::{Error, Result, ParseError};
1212

1313
static TAG_PREFIX: &'static str = "a";
1414
const INITIAL_TAG: u32 = 0;
@@ -464,7 +464,7 @@ impl<T: Read + Write> Client<T> {
464464

465465
while !found_tag_line {
466466
let raw_data = try!(self.readline());
467-
let line = String::from_utf8(raw_data).unwrap();
467+
let line = String::from_utf8(raw_data).map_err(|err| Error::Parse(ParseError::DataNotUtf8(err)))?;
468468
lines.push(line.clone());
469469
if (&*line).starts_with(&*start_str) {
470470
found_tag_line = true;

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::result;
33
use std::fmt;
44
use std::error::Error as StdError;
55
use std::net::TcpStream;
6+
use std::string::FromUtf8Error;
67

78
use native_tls::HandshakeError as TlsHandshakeError;
89
use native_tls::Error as TlsError;
@@ -85,6 +86,7 @@ impl StdError for Error {
8586
Error::Io(ref e) => Some(e),
8687
Error::Tls(ref e) => Some(e),
8788
Error::TlsHandshake(ref e) => Some(e),
89+
Error::Parse(ParseError::DataNotUtf8(ref e)) => Some(e),
8890
_ => None,
8991
}
9092
}
@@ -98,6 +100,7 @@ pub enum ParseError {
98100
Capability(Vec<String>),
99101
// Authentication errors.
100102
Authentication(String),
103+
DataNotUtf8(FromUtf8Error),
101104
}
102105

103106
impl fmt::Display for ParseError {
@@ -114,6 +117,7 @@ impl StdError for ParseError {
114117
ParseError::StatusResponse(_) => "Unable to parse status response",
115118
ParseError::Capability(_) => "Unable to parse capability response",
116119
ParseError::Authentication(_) => "Unable to parse authentication response",
120+
ParseError::DataNotUtf8(_) => "Unable to parse data as UTF-8 text",
117121
}
118122
}
119123

0 commit comments

Comments
 (0)