use std::fmt;
#[derive(Debug)]
pub enum HttpError {
Protocol(&'static str),
LimitExceeded(&'static str),
Io(std::io::Error),
}
impl HttpError {
pub const fn class(&self) -> &'static str {
match self {
Self::Protocol(_) => "protocol",
Self::LimitExceeded(_) => "limit",
Self::Io(_) => "io",
}
}
pub const fn is_retryable(&self) -> bool {
matches!(self, Self::Io(_))
}
}
impl fmt::Display for HttpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Protocol(what) => write!(f, "invalid HTTP response: {what}"),
Self::LimitExceeded(what) => write!(f, "HTTP response exceeded {what}"),
Self::Io(error) => write!(f, "transport error: {error}"),
}
}
}
impl std::error::Error for HttpError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(error) => Some(error),
_ => None,
}
}
}
impl From<std::io::Error> for HttpError {
fn from(error: std::io::Error) -> Self {
Self::Io(error)
}
}