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: Option<http::Method>,
url: Option<Url>,
) -> HttpError {
let kind =
classify_reqwest_error_kind(error.is_timeout(), error.is_decode(), phase, default_kind);
let mut result = HttpError::new(kind, format!("HTTP transport error: {}", error));
if let Some(method) = method {
result = result.with_method(&method);
}
if let Some(url) = url {
result = result.with_url(&url);
}
result.with_source(error)
}
fn classify_reqwest_error_kind(
is_timeout: bool,
is_decode: bool,
phase: ReqwestErrorPhase,
default_kind: HttpErrorKind,
) -> HttpErrorKind {
if is_timeout {
classify_timeout_kind(phase)
} else if is_decode {
HttpErrorKind::Decode
} else {
default_kind
}
}
fn classify_timeout_kind(phase: ReqwestErrorPhase) -> HttpErrorKind {
match phase {
ReqwestErrorPhase::Send => HttpErrorKind::RequestTimeout,
ReqwestErrorPhase::Read => HttpErrorKind::ReadTimeout,
}
}