Try connection instead of unwrapping

Return an error instead of panicking on connection errors, which allows to
handle the problem from the caller.

fixes #12
This commit is contained in:
Miquel Ruiz 2016-06-05 12:47:46 +01:00
parent 0cbe8dbd10
commit 6816b922f0

View file

@ -31,7 +31,7 @@ impl IMAPStream {
pub fn connect<S: Into<String>>(host: S, port: u16, ssl_context: Option<SslContext>) -> Result<IMAPStream> {
let host_string = host.into();
let connect_string = format!("{}:{}", host_string, port);
let tcp_stream = TcpStream::connect(&*connect_string).unwrap();
let tcp_stream = try!(TcpStream::connect(&*connect_string));
let mut socket = match ssl_context {
Some(context) => IMAPStream { stream: IMAPStreamTypes::Ssl(SslStream::connect(&context, tcp_stream).unwrap()), host: host_string, port: port, tag: 1, tag_prefix: "a"},
None => IMAPStream { stream: IMAPStreamTypes::Basic(tcp_stream), host: host_string, port: port, tag: 1, tag_prefix: "a"},
@ -334,3 +334,9 @@ impl IMAPStream {
return command;
}
}
#[test]
fn connect() {
let imap = IMAPStream::connect("this-is-not-an-imap-server", 143, None);
assert!(imap.is_err());
}