use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Protocol(String),
Reset(u32),
Closed,
Refused,
Timeout,
Io(String),
Tls(String),
Url(String),
Decode(String),
TooManyRedirects,
TooLarge,
Status(u16),
ProxyAuth,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Protocol(reason) => write!(f, "protocol error: {reason}"),
Error::Reset(code) => write!(f, "exchange reset by peer with code {code}"),
Error::Closed => f.write_str("connection closed"),
Error::Refused => f.write_str("exchange refused by peer before processing"),
Error::Timeout => f.write_str("timed out"),
Error::Io(e) => write!(f, "i/o error: {e}"),
Error::Tls(e) => write!(f, "tls error: {e}"),
Error::Url(e) => write!(f, "invalid url: {e}"),
Error::Decode(e) => write!(f, "could not decode body: {e}"),
Error::TooManyRedirects => f.write_str("too many redirects"),
Error::TooLarge => f.write_str("response body too large"),
Error::Status(code) => write!(f, "status {code}"),
Error::ProxyAuth => f.write_str("the proxy requires authentication"),
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
if e.kind() == std::io::ErrorKind::TimedOut {
return Error::Timeout;
}
Error::Io(e.to_string())
}
}