tests: handle errors properly in examples (#748)

This commit is contained in:
Liran Ringel
2018-11-20 11:10:36 -05:00
committed by Toby Lawrence
parent 477fa5580a
commit 9b1a45cc6a
19 changed files with 144 additions and 119 deletions
+6 -5
View File
@@ -12,12 +12,12 @@ use native_tls::TlsConnector;
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
fn main() {
let mut runtime = Runtime::new().unwrap();
let addr = "www.rust-lang.org:443".to_socket_addrs().unwrap().next().unwrap();
fn main() -> Result<(), Box<std::error::Error>> {
let mut runtime = Runtime::new()?;
let addr = "www.rust-lang.org:443".to_socket_addrs()?.next().ok_or("failed to resolve www.rust-lang.org")?;
let socket = TcpStream::connect(&addr);
let cx = TlsConnector::builder().build().unwrap();
let cx = TlsConnector::builder().build()?;
let cx = tokio_tls::TlsConnector::from(cx);
let tls_handshake = socket.and_then(move |socket| {
@@ -36,6 +36,7 @@ fn main() {
tokio_io::io::read_to_end(socket, Vec::new())
});
let (_, data) = runtime.block_on(response).unwrap();
let (_, data) = runtime.block_on(response)?;
println!("{}", String::from_utf8_lossy(&data));
Ok(())
}
+6 -5
View File
@@ -8,16 +8,16 @@ use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;
fn main() {
fn main() -> Result<(), Box<std::error::Error>> {
// Bind the server's socket
let addr = "127.0.0.1:12345".parse().unwrap();
let tcp = TcpListener::bind(&addr).unwrap();
let addr = "127.0.0.1:12345".parse()?;
let tcp = TcpListener::bind(&addr)?;
// Create the TLS acceptor.
let der = include_bytes!("identity.p12");
let cert = Identity::from_pkcs12(der, "mypass").unwrap();
let cert = Identity::from_pkcs12(der, "mypass")?;
let tls_acceptor = tokio_tls::TlsAcceptor::from(
native_tls::TlsAcceptor::builder(cert).build().unwrap());
native_tls::TlsAcceptor::builder(cert).build()?);
// Iterate incoming connections
let server = tcp.incoming().for_each(move |tcp| {
@@ -56,4 +56,5 @@ fn main() {
// Start the runtime and spin up the server
tokio::run(server);
Ok(())
}