hreq_h1/
error.rs

1use std::fmt;
2use std::io;
3
4/// Possible errors from this crate.
5#[derive(Debug)]
6pub enum Error {
7    /// A user/usage problem such as sending more bytes than a content-length header specifies.
8    User(String),
9    /// A wrapped std::io::Error from the underlying transport (socket).
10    Io(io::Error),
11    /// HTTP/1.1 parse errors from the `httparse` crate.
12    Http11Parser(httparse::Error),
13    /// Http errors from the `http` crate.
14    Http(http::Error),
15}
16
17impl Error {
18    pub(crate) fn into_io(self) -> io::Error {
19        match self {
20            Error::Io(i) => i,
21            Error::User(e) => io::Error::new(io::ErrorKind::Other, e),
22            Error::Http11Parser(e) => io::Error::new(io::ErrorKind::Other, e),
23            Error::Http(e) => io::Error::new(io::ErrorKind::Other, e),
24        }
25    }
26}
27
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        match self {
31            Error::User(v) => write!(f, "{}", v),
32            Error::Io(v) => fmt::Display::fmt(v, f),
33            Error::Http11Parser(v) => write!(f, "http11 parser: {}", v),
34            Error::Http(v) => write!(f, "http api: {}", v),
35        }
36    }
37}
38
39impl std::error::Error for Error {}
40
41impl From<io::Error> for Error {
42    fn from(e: io::Error) -> Self {
43        Error::Io(e)
44    }
45}
46
47impl From<httparse::Error> for Error {
48    fn from(e: httparse::Error) -> Self {
49        Error::Http11Parser(e)
50    }
51}
52
53impl From<http::Error> for Error {
54    fn from(e: http::Error) -> Self {
55        Error::Http(e)
56    }
57}