use url::Url;
use super::ReqwestErrorPhase;
use crate::{
HttpError,
HttpErrorKind,
};
pub(crate) fn map_reqwest_error(
error: reqwest::Error,
default_kind: HttpErrorKind,
phase: ReqwestErrorPhase,
method: http::Method,
url: Url,
) -> HttpError {
let is_timeout = error.is_timeout();
let error = error.without_url();
let kind = classify_reqwest_error_kind(is_timeout, phase, default_kind);
HttpError::new(kind, format!("HTTP transport error: {}", error))
.with_method(&method)
.with_url(&url)
.with_source(error)
}
fn classify_reqwest_error_kind(
is_timeout: bool,
phase: ReqwestErrorPhase,
default_kind: HttpErrorKind,
) -> HttpErrorKind {
if is_timeout {
classify_timeout_kind(phase)
} else {
default_kind
}
}
fn classify_timeout_kind(phase: ReqwestErrorPhase) -> HttpErrorKind {
match phase {
ReqwestErrorPhase::Send => HttpErrorKind::RequestTimeout,
ReqwestErrorPhase::Read => HttpErrorKind::ReadTimeout,
}
}