monoio_http/common/
error.rs

1use std::{
2    convert::Infallible,
3    hint::unreachable_unchecked,
4    io::{Error, ErrorKind},
5};
6
7use thiserror::Error as ThisError;
8
9use crate::h1::{
10    codec::{decoder::DecodeError, encoder::EncodeError},
11    payload::PayloadError,
12};
13
14#[cfg(feature = "parsed")]
15#[derive(ThisError, Debug)]
16pub enum ParseError {
17    #[error("Uninitialized cookie jar")]
18    UninitializedCookieJar,
19    #[error("http cookie parsing error {0}")]
20    CookieParseError(#[from] cookie::ParseError),
21    #[error("Invalid Header Value")]
22    InvalidHeaderValue,
23    #[error("Invalid content type")]
24    InvalidContentType,
25    #[error("SerDe error {0}")]
26    Serde(#[from] serde_urlencoded::de::Error),
27    #[error("Previous Error")]
28    Previous,
29    #[error("http error {0}")]
30    Http(#[from] HttpError),
31    #[error("Multer Error")]
32    MulterError(#[from] multer::Error),
33    #[error("IO error {0}")]
34    IOError(#[from] std::io::Error),
35}
36
37#[derive(ThisError, Debug)]
38pub enum HttpError {
39    #[error("http1 Encode error {0}")]
40    H1EncodeError(#[from] EncodeError),
41    #[error("http2 decode error {0}")]
42    H1DecodeError(#[from] DecodeError),
43    #[error("receive body error {0}")]
44    PayloadError(#[from] PayloadError),
45    #[error("H2 error {0}")]
46    H2Error(#[from] crate::h2::Error),
47    #[error("IO error {0}")]
48    IOError(#[from] std::io::Error),
49    // #[cfg(feature = "parsed")]
50    // #[error("Parse error {0}")]
51    // ParseError(#[from] ParseError),
52}
53
54impl From<Infallible> for HttpError {
55    fn from(_: Infallible) -> Self {
56        unsafe { unreachable_unchecked() }
57    }
58}
59
60impl Clone for HttpError {
61    fn clone(&self) -> Self {
62        match self {
63            Self::IOError(e) => Self::IOError(Error::new(ErrorKind::Other, e.to_string())),
64            _ => self.clone(),
65        }
66    }
67}
68
69#[derive(ThisError, Debug)]
70pub enum EncodeDecodeError<T> {
71    #[error("encode/decode error {0}")]
72    EncodeDecode(#[from] std::io::Error),
73    #[error("http error {0}")]
74    Http(T),
75}