1use std::fmt;
5
6#[derive(Debug)]
8pub enum HttpError {
9 Protocol(&'static str),
11 LimitExceeded(&'static str),
13 Io(std::io::Error),
15}
16
17impl HttpError {
18 pub const fn class(&self) -> &'static str {
20 match self {
21 Self::Protocol(_) => "protocol",
22 Self::LimitExceeded(_) => "limit",
23 Self::Io(_) => "io",
24 }
25 }
26
27 pub const fn is_retryable(&self) -> bool {
30 matches!(self, Self::Io(_))
31 }
32}
33
34impl fmt::Display for HttpError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 Self::Protocol(what) => write!(f, "invalid HTTP response: {what}"),
38 Self::LimitExceeded(what) => write!(f, "HTTP response exceeded {what}"),
39 Self::Io(error) => write!(f, "transport error: {error}"),
40 }
41 }
42}
43
44impl std::error::Error for HttpError {
45 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46 match self {
47 Self::Io(error) => Some(error),
48 _ => None,
49 }
50 }
51}
52
53impl From<std::io::Error> for HttpError {
54 fn from(error: std::io::Error) -> Self {
55 Self::Io(error)
56 }
57}