use std::fmt;
use http_body_util::BodyExt;
use hyper::body::Incoming;
use crate::http1_to_02_status_code;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub enum Error {
Generic(String),
Http(HttpErrorContent<crate::models::HttpErrorOut>),
Validation(HttpErrorContent<crate::models::HttpValidationError>),
}
impl Error {
pub(crate) fn generic(err: impl std::error::Error) -> Self {
Self::Generic(format!("{err:?}"))
}
pub fn status_code(&self) -> Option<u16> {
match self {
Error::Http(e) => Some(e.status.as_u16()),
Error::Validation(_) => Some(422),
Error::Generic(_) => None,
}
}
pub fn headers(&self) -> Option<&std::collections::HashMap<String, String>> {
match self {
Error::Http(e) => e.headers.as_ref(),
Error::Validation(e) => e.headers.as_ref(),
Error::Generic(_) => None,
}
}
pub fn is_bad_request(&self) -> bool { self.status_code() == Some(400) }
pub fn is_unauthorized(&self) -> bool { self.status_code() == Some(401) }
pub fn is_forbidden(&self) -> bool { self.status_code() == Some(403) }
pub fn is_not_found(&self) -> bool { self.status_code() == Some(404) }
pub fn is_conflict(&self) -> bool { self.status_code() == Some(409) }
pub fn is_validation_error(&self) -> bool { self.status_code() == Some(422) }
pub fn is_rate_limited(&self) -> bool { self.status_code() == Some(429) }
pub fn is_internal_server_error(&self) -> bool { self.status_code() == Some(500) }
pub fn is_bad_gateway(&self) -> bool { self.status_code() == Some(502) }
pub fn is_service_unavailable(&self) -> bool { self.status_code() == Some(503) }
pub fn is_gateway_timeout(&self) -> bool { self.status_code() == Some(504) }
pub fn is_server_error(&self) -> bool { self.status_code().map_or(false, |s| s >= 500) }
pub fn is_client_error(&self) -> bool { self.status_code().map_or(false, |s| s >= 400 && s < 500) }
pub fn retry_after(&self) -> Option<u64> {
self.headers()
.and_then(|h| h.get("retry-after"))
.and_then(|v| v.parse().ok())
}
pub(crate) async fn from_response(
status_code: http1::StatusCode,
body: Incoming,
headers: Option<&http1::HeaderMap>,
) -> Self {
let header_map = headers.map(|h| {
let mut map = std::collections::HashMap::new();
for (k, v) in h.iter() {
if let Ok(val) = v.to_str() {
map.insert(k.as_str().to_string(), val.to_string());
}
}
map
});
match body.collect().await {
Ok(collected) => {
let bytes = collected.to_bytes();
if status_code == http1::StatusCode::UNPROCESSABLE_ENTITY {
Self::Validation(HttpErrorContent {
status: http02::StatusCode::UNPROCESSABLE_ENTITY,
payload: serde_json::from_slice(&bytes).ok(),
headers: header_map,
})
} else {
Error::Http(HttpErrorContent {
status: http1_to_02_status_code(status_code),
payload: serde_json::from_slice(&bytes).ok(),
headers: header_map,
})
}
}
Err(e) => Self::Generic(e.to_string()),
}
}
}
impl From<Error> for String {
fn from(err: Error) -> Self {
err.to_string()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Generic(s) => s.fmt(f),
Error::Http(e) => format!("Http error (status={}) {:?}", e.status, e.payload).fmt(f),
Error::Validation(e) => format!("Validation error {:?}", e.payload).fmt(f),
}
}
}
impl std::error::Error for Error {}
#[derive(Debug, Clone)]
pub struct HttpErrorContent<T> {
pub status: http02::StatusCode,
pub payload: Option<T>,
pub headers: Option<std::collections::HashMap<String, String>>,
}
impl Error {
pub fn timeout(message: impl Into<String>) -> Self {
Self::Generic(format!("Timeout: {}", message.into()))
}
pub fn network(message: impl Into<String>) -> Self {
Self::Generic(format!("Network error: {}", message.into()))
}
pub fn authentication(message: impl Into<String>) -> Self {
Self::Generic(format!("Authentication error: {}", message.into()))
}
pub fn is_request_timeout(&self) -> bool { self.status_code() == Some(408) }
pub fn is_gone(&self) -> bool { self.status_code() == Some(410) }
pub fn is_payload_too_large(&self) -> bool { self.status_code() == Some(413) }
pub fn is_not_implemented(&self) -> bool { self.status_code() == Some(501) }
pub fn is_insufficient_storage(&self) -> bool { self.status_code() == Some(507) }
pub fn is_loop_detected(&self) -> bool { self.status_code() == Some(508) }
}