use super::request::RequestedHttpVersion;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HttpError {
CouldNotParseCookieExpires(String),
CouldNotParseResponse,
CouldNotUncompressResponse {
description: String,
},
InvalidCharset {
charset: String,
},
InvalidDecoding {
charset: String,
},
Libcurl {
code: i32,
description: String,
},
LibcurlUnknownOption {
option: String,
minimum_version: String,
},
NoPrimaryIp,
TooManyRedirect,
UnsupportedContentEncoding {
description: String,
},
UnsupportedHttpVersion(RequestedHttpVersion),
InvalidUrl(String, String),
AllowedResponseSizeExceeded(u64),
}
impl From<curl::Error> for HttpError {
fn from(err: curl::Error) -> Self {
let code = err.code() as i32;
let description = err.description().to_string();
HttpError::Libcurl { code, description }
}
}
impl From<curl::FormError> for HttpError {
fn from(err: curl::FormError) -> Self {
let code = err.code() as i32;
let description = err.description().to_string();
HttpError::Libcurl { code, description }
}
}
impl HttpError {
pub fn description(&self) -> String {
match self {
HttpError::AllowedResponseSizeExceeded(_) => "HTTP connection".to_string(),
HttpError::CouldNotParseCookieExpires(_) => "HTTP connection".to_string(),
HttpError::CouldNotParseResponse => "HTTP connection".to_string(),
HttpError::CouldNotUncompressResponse { .. } => "Decompression error".to_string(),
HttpError::InvalidCharset { .. } => "Invalid charset".to_string(),
HttpError::InvalidDecoding { .. } => "Invalid decoding".to_string(),
HttpError::InvalidUrl(..) => "Invalid URL".to_string(),
HttpError::Libcurl { .. } => "HTTP connection".to_string(),
HttpError::LibcurlUnknownOption { .. } => "HTTP connection".to_string(),
HttpError::NoPrimaryIp => "HTTP connection".to_string(),
HttpError::TooManyRedirect => "HTTP connection".to_string(),
HttpError::UnsupportedContentEncoding { .. } => "Decompression error".to_string(),
HttpError::UnsupportedHttpVersion(_) => "Unsupported HTTP version".to_string(),
}
}
pub fn message(&self) -> String {
match self {
HttpError::AllowedResponseSizeExceeded(max_size) => {
format!("exceeded the maximum allowed file size ({max_size} bytes)")
}
HttpError::CouldNotParseCookieExpires(value) => {
format!("could not parse Cookie Expires attribute value <{value}>")
}
HttpError::CouldNotParseResponse => "could not parse Response".to_string(),
HttpError::CouldNotUncompressResponse { description } => {
format!("could not uncompress response with {description}")
}
HttpError::InvalidCharset { charset } => {
format!("the charset '{charset}' is not valid")
}
HttpError::InvalidDecoding { charset } => {
format!("could not decode response body with charset '{charset}'")
}
HttpError::InvalidUrl(url, reason) => {
format!("invalid URL <{url}> ({reason})").to_string()
}
HttpError::Libcurl { code, description } => format!("({code}) {description}"),
HttpError::LibcurlUnknownOption {
option,
minimum_version,
} => format!("Option {option} requires libcurl version {minimum_version} or higher"),
HttpError::NoPrimaryIp => "No primary IP found in response".to_string(),
HttpError::TooManyRedirect => "too many redirect".to_string(),
HttpError::UnsupportedHttpVersion(version) => {
format!("{version} is not supported, check --version").to_string()
}
HttpError::UnsupportedContentEncoding { description } => {
format!("compression {description} is not supported").to_string()
}
}
}
}