Skip to content

Commit b9d1ccc

Browse files
committed
Drop enum, use DenoError
1 parent 9da5589 commit b9d1ccc

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

src/http.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
22

3+
use errors;
34
use errors::{DenoError, DenoResult};
45
use futures;
56
use futures::future::Either;
@@ -12,7 +13,6 @@ use hyper::client::Client;
1213
use hyper::client::HttpConnector;
1314
use hyper::Uri;
1415
use hyper_rustls;
15-
use std::io;
1616

1717
type Connector = hyper_rustls::HttpsConnector<HttpConnector>;
1818

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

32-
enum HyperOrIOError {
33-
IO(io::Error),
34-
Hyper(hyper::Error),
35-
}
36-
37-
fn response_future(
38-
response: hyper::Response<hyper::Body>,
39-
) -> impl Future<Item = String, Error = HyperOrIOError> {
40-
if !response.status().is_success() {
41-
return Either::A(futures::future::err(HyperOrIOError::IO(io::Error::new(
42-
io::ErrorKind::NotFound,
43-
format!("module not found"),
44-
))));
45-
}
46-
Either::B(
47-
response
48-
.into_body()
49-
.concat2()
50-
.map(|body| String::from_utf8(body.to_vec()).unwrap())
51-
.map_err(|err| HyperOrIOError::Hyper(err)),
52-
)
53-
}
54-
5532
// The CodeFetch message is used to load HTTP javascript resources and expects a
5633
// synchronous response, this utility method supports that.
5734
pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> {
5835
let url = module_name.parse::<Uri>().unwrap();
5936
let client = get_client();
6037
let fetch_future = client
6138
.get(url)
62-
.map_err(|err| HyperOrIOError::Hyper(err))
63-
.and_then(response_future);
64-
match tokio_util::block_on(fetch_future) {
65-
Ok(s) => Ok(s),
66-
Err(HyperOrIOError::Hyper(err)) => Err(DenoError::from(err)),
67-
Err(HyperOrIOError::IO(err)) => Err(DenoError::from(err)),
68-
}
39+
.map_err(|err| DenoError::from(err))
40+
.and_then(|response| {
41+
if !response.status().is_success() {
42+
return Either::A(futures::future::err(errors::new(
43+
errors::ErrorKind::NotFound,
44+
"module not found".to_string(),
45+
)));
46+
}
47+
Either::B(
48+
response
49+
.into_body()
50+
.concat2()
51+
.map(|body| String::from_utf8(body.to_vec()).unwrap())
52+
.map_err(|err| DenoError::from(err)),
53+
)
54+
});
55+
tokio_util::block_on(fetch_future)
6956
}
7057

7158
/* TODO(ry) Re-enabled this test. Disabling to work around bug in #782.

0 commit comments

Comments
 (0)