Skip to content

Commit

Permalink
Drop enum, use DenoError
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Oct 1, 2018
1 parent 9da5589 commit b9d1ccc
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<HttpConnector>;

Expand All @@ -29,43 +29,30 @@ pub fn get_client() -> Client<Connector, hyper::Body> {
Client::builder().build(c)
}

enum HyperOrIOError {
IO(io::Error),
Hyper(hyper::Error),
}

fn response_future(
response: hyper::Response<hyper::Body>,
) -> impl Future<Item = String, Error = HyperOrIOError> {
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<String> {
let url = module_name.parse::<Uri>().unwrap();
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.
Expand Down

0 comments on commit b9d1ccc

Please sign in to comment.