diff options
author | Liran Ringel <5730310+liranringel@users.noreply.github.com> | 2018-11-20 18:10:36 +0200 |
---|---|---|
committer | Toby Lawrence <tobz@users.noreply.github.com> | 2018-11-20 11:10:36 -0500 |
commit | 9b1a45cc6a15f5d2be17531dffc2f50d2b019646 (patch) | |
tree | da66c5c9574f2cd7ad11745e414fc34da2e35c6f /examples/tinyhttp.rs | |
parent | 477fa5580aa3796f97e3e0eb1325d4690b3b4e96 (diff) |
tests: handle errors properly in examples (#748)
Diffstat (limited to 'examples/tinyhttp.rs')
-rw-r--r-- | examples/tinyhttp.rs | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs index 1e4f22bd..7a80d730 100644 --- a/examples/tinyhttp.rs +++ b/examples/tinyhttp.rs @@ -34,13 +34,13 @@ use bytes::BytesMut; use http::header::HeaderValue; use http::{Request, Response, StatusCode}; -fn main() { +fn main() -> Result<(), Box<std::error::Error>> { // Parse the arguments, bind the TCP socket we'll be listening to, spin up // our worker threads, and start shipping sockets to those worker threads. let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); - let addr = addr.parse::<SocketAddr>().unwrap(); + let addr = addr.parse::<SocketAddr>()?; - let listener = TcpListener::bind(&addr).expect("failed to bind"); + let listener = TcpListener::bind(&addr)?; println!("Listening on: {}", addr); tokio::run({ @@ -51,6 +51,7 @@ fn main() { Ok(()) }) }); + Ok(()) } fn process(socket: TcpStream) { @@ -84,28 +85,32 @@ fn process(socket: TcpStream) { fn respond(req: Request<()>) -> Box<Future<Item = Response<String>, Error = io::Error> + Send> { - let mut ret = Response::builder(); - let body = match req.uri().path() { - "/plaintext" => { - ret.header("Content-Type", "text/plain"); - "Hello, World!".to_string() - } - "/json" => { - ret.header("Content-Type", "application/json"); + let f = future::lazy(move || { + let mut response = Response::builder(); + let body = match req.uri().path() { + "/plaintext" => { + response.header("Content-Type", "text/plain"); + "Hello, World!".to_string() + } + "/json" => { + response.header("Content-Type", "application/json"); - #[derive(Serialize)] - struct Message { - message: &'static str, + #[derive(Serialize)] + struct Message { + message: &'static str, + } + serde_json::to_string(&Message { message: "Hello, World!" })? } - serde_json::to_string(&Message { message: "Hello, World!" }) - .unwrap() - } - _ => { - ret.status(StatusCode::NOT_FOUND); - String::new() - } - }; - Box::new(future::ok(ret.body(body).unwrap())) + _ => { + response.status(StatusCode::NOT_FOUND); + String::new() + } + }; + let response = response.body(body).map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; + Ok(response) + }); + + Box::new(f) } struct Http; |