1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{
    convert::Infallible,
    hint::unreachable_unchecked,
    io::{Error, ErrorKind},
};

use thiserror::Error as ThisError;

use crate::h1::{
    codec::{decoder::DecodeError, encoder::EncodeError},
    payload::PayloadError,
};

#[cfg(feature = "parsed")]
#[derive(ThisError, Debug)]
pub enum ParseError {
    #[error("Uninitialized cookie jar")]
    UninitializedCookieJar,
    #[error("http cookie parsing error {0}")]
    CookieParseError(#[from] cookie::ParseError),
    #[error("Invalid Header Value")]
    InvalidHeaderValue,
    #[error("Invalid content type")]
    InvalidContentType,
    #[error("SerDe error {0}")]
    Serde(#[from] serde_urlencoded::de::Error),
    #[error("http error {0}")]
    Http(#[from] HttpError),
    #[error("Previous returned error")]
    Previous,
}

#[derive(ThisError, Debug)]
pub enum HttpError {
    #[error("http1 Encode error {0}")]
    H1EncodeError(#[from] EncodeError),
    #[error("http2 decode error {0}")]
    H1DecodeError(#[from] DecodeError),
    #[error("receive body error {0}")]
    PayloadError(#[from] PayloadError),
    #[error("H2 error {0}")]
    H2Error(#[from] crate::h2::Error),
    #[error("IO error {0}")]
    IOError(#[from] std::io::Error),
}

impl From<Infallible> for HttpError {
    fn from(_: Infallible) -> Self {
        unsafe { unreachable_unchecked() }
    }
}

impl Clone for HttpError {
    fn clone(&self) -> Self {
        match self {
            Self::IOError(e) => Self::IOError(Error::new(ErrorKind::Other, e.to_string())),
            _ => self.clone(),
        }
    }
}

#[derive(ThisError, Debug)]
pub enum EncodeDecodeError<T> {
    #[error("encode/decode error {0}")]
    EncodeDecode(#[from] std::io::Error),
    #[error("http error {0}")]
    Http(T),
}