diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2018-10-02 09:38:45 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-10-02 12:38:45 -0400 |
commit | eba58b718860035b830d0739a02c6a382e1f3307 (patch) | |
tree | 353e9d10a7efae1bc75b9a5392944e02cb11c02a /src/http.rs | |
parent | fc1c54dde052d6a967d5f185604b3f79b1ba2fbb (diff) |
Guess extensions on extension not provided (#859)
Fixes #857
Diffstat (limited to 'src/http.rs')
-rw-r--r-- | src/http.rs | 30 |
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. |