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 70270a0
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 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,26 +29,21 @@ 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> {
) -> impl Future<Item = String, Error = DenoError> {
if !response.status().is_success() {
return Either::A(futures::future::err(HyperOrIOError::IO(io::Error::new(
io::ErrorKind::NotFound,
format!("module not found"),
))));
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| HyperOrIOError::Hyper(err)),
.map_err(|err| DenoError::from(err)),
)
}

Expand All @@ -59,13 +54,9 @@ pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> {
let client = get_client();
let fetch_future = client
.get(url)
.map_err(|err| HyperOrIOError::Hyper(err))
.map_err(|err| DenoError::from(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)),
}
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 70270a0

Please sign in to comment.