Hi, I have use hyper-timeout crate to have custom timeout with hyper 0.11 but since I upgrade to hyper 0.12 I struggle to have this function implemented in my project.
I tried to use the example provide here but I can't get rid of the "expected struct std::io::Error, found struct hyper::Error error.
I tried to map_err to a custom error and I get an errors than even more elusive to me.
let response: Mutex<(StatusCode, HeaderMap)> =
Mutex::new((StatusCode::from_u16(501).unwrap(), HeaderMap::new()));
let work = client
.request(req)
.and_then(|res| {
match response.lock() {
Ok(mut response) => {
response.0 = res.status();
response.1 = res.headers().clone()
}
Err(_) => {}
}
res.into_body().fold(Vec::new(), |mut vec, chunk| {
vec.extend(&chunk[..]);
futures::future::ok::<_, HyperError>(vec)
})
})
.select2(Timeout::new(Duration::from_secs(timeout), &core.handle())?)
.then(|result| match result {
Ok(Either::A((got, _timeout))) => {
let headers;
let status;
match response.lock() {
Ok(res) => {
status = res.0;
headers = res.1.clone();
}
Err(_) => {
status = StatusCode::from_u16(501).unwrap();
headers = HeaderMap::new();
}
}
Ok(HttpResponse {
status,
body: got,
headers,
uri: None,
})
}
Ok(Either::B((_timeout_error, _get))) => {
return Err(MyError::new(-1, "Client timed out while connecting"));
}
Err(Either::A((get_error, _timeout))) => Err(MyError::from(get_error)),
Err(Either::B((timeout_error, _get))) => Err(MyError::from(timeout_error)),
});l
I have to use a Mutex and recreate an error each time, I don't think it's the best way to do it. I tried to Box the errors but got mismatch expect type error. Tried to return tuple of body response and header response select2 complains about not implemented trait. If someone can come with better implementation.