From b9d1cccf2d6691621c970a1b3363339c0b23a681 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Mon, 1 Oct 2018 12:23:12 -0700 Subject: [PATCH] Drop enum, use DenoError --- src/http.rs | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/src/http.rs b/src/http.rs index 985e6dbda7143e..798b9ad25089e3 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,5 +1,6 @@ // Copyright 2018 the Deno authors. All rights reserved. MIT license. +use errors; use errors::{DenoError, DenoResult}; use futures; use futures::future::Either; @@ -12,7 +13,6 @@ use hyper::client::Client; use hyper::client::HttpConnector; use hyper::Uri; use hyper_rustls; -use std::io; type Connector = hyper_rustls::HttpsConnector; @@ -29,29 +29,6 @@ pub fn get_client() -> Client { Client::builder().build(c) } -enum HyperOrIOError { - IO(io::Error), - Hyper(hyper::Error), -} - -fn response_future( - response: hyper::Response, -) -> impl Future { - if !response.status().is_success() { - return Either::A(futures::future::err(HyperOrIOError::IO(io::Error::new( - io::ErrorKind::NotFound, - format!("module not found"), - )))); - } - Either::B( - response - .into_body() - .concat2() - .map(|body| String::from_utf8(body.to_vec()).unwrap()) - .map_err(|err| HyperOrIOError::Hyper(err)), - ) -} - // The CodeFetch message is used to load HTTP javascript resources and expects a // synchronous response, this utility method supports that. pub fn fetch_sync_string(module_name: &str) -> DenoResult { @@ -59,13 +36,23 @@ pub fn fetch_sync_string(module_name: &str) -> DenoResult { let client = get_client(); let fetch_future = client .get(url) - .map_err(|err| HyperOrIOError::Hyper(err)) - .and_then(response_future); - match tokio_util::block_on(fetch_future) { - Ok(s) => Ok(s), - Err(HyperOrIOError::Hyper(err)) => Err(DenoError::from(err)), - Err(HyperOrIOError::IO(err)) => Err(DenoError::from(err)), - } + .map_err(|err| DenoError::from(err)) + .and_then(|response| { + if !response.status().is_success() { + return Either::A(futures::future::err(errors::new( + errors::ErrorKind::NotFound, + "module not found".to_string(), + ))); + } + Either::B( + response + .into_body() + .concat2() + .map(|body| String::from_utf8(body.to_vec()).unwrap()) + .map_err(|err| DenoError::from(err)), + ) + }); + tokio_util::block_on(fetch_future) } /* TODO(ry) Re-enabled this test. Disabling to work around bug in #782.