summaryrefslogtreecommitdiff
path: root/src/http.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.rs')
-rw-r--r--src/http.rs30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/http.rs b/src/http.rs
index 5f78cf232..9ef9adb8f 100644
--- a/src/http.rs
+++ b/src/http.rs
@@ -1,10 +1,12 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
-use errors::DenoResult;
+use errors;
+use errors::{DenoError, DenoResult};
use tokio_util;
-use futures::Future;
-use futures::Stream;
+use futures;
+use futures::future::Either;
+use futures::{Future, Stream};
use hyper;
use hyper::client::Client;
use hyper::client::HttpConnector;
@@ -31,11 +33,25 @@ pub fn get_client() -> Client<Connector, hyper::Body> {
pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> {
let url = module_name.parse::<Uri>().unwrap();
let client = get_client();
- let future = client
+ let fetch_future = client
.get(url)
- .and_then(|response| response.into_body().concat2());
- let body = tokio_util::block_on(future)?;
- Ok(String::from_utf8(body.to_vec()).unwrap())
+ .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.