Skip to main content

http_req/
error.rs

1//! error system used around the library.
2
3use std::{error, fmt, io, num, str, sync::mpsc};
4
5/// Enum representing different parsing errors encountered by the library.
6#[derive(Debug, PartialEq)]
7pub enum ParseErr {
8    /// Error related to invalid UTF-8 character sequences encountered
9    /// during string processing or conversion operations.
10    Utf8(str::Utf8Error),
11
12    /// Failure in parsing integer values from strings using standard
13    /// number formats, such as those conforming to base 10 conventions.
14    Int(num::ParseIntError),
15
16    /// Issue encountered when processing status line from HTTP response message.
17    StatusErr,
18
19    /// Issue encountered when processing headers from HTTP response message.
20    HeadersErr,
21
22    /// Issue arising while processing URIs that contain invalid
23    /// characters or do not follow the URI specification.
24    UriErr,
25
26    /// Error indicating that provided string, vector, or other element
27    /// does not contain any values that could be parsed.
28    Empty,
29}
30
31impl error::Error for ParseErr {
32    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
33        use self::ParseErr::*;
34
35        match self {
36            Utf8(e) => Some(e),
37            Int(e) => Some(e),
38            _ => None,
39        }
40    }
41}
42
43impl fmt::Display for ParseErr {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        use self::ParseErr::*;
46
47        let err = match self {
48            Utf8(_) => "Invalid character sequence",
49            Int(_) => "Cannot parse number",
50            StatusErr => "Status line contains invalid values",
51            HeadersErr => "Headers contain invalid values",
52            UriErr => "URI contains invalid characters",
53            Empty => "Nothing to parse",
54        };
55        write!(f, "ParseErr: {}", err)
56    }
57}
58
59impl From<num::ParseIntError> for ParseErr {
60    fn from(e: num::ParseIntError) -> Self {
61        ParseErr::Int(e)
62    }
63}
64
65impl From<str::Utf8Error> for ParseErr {
66    fn from(e: str::Utf8Error) -> Self {
67        ParseErr::Utf8(e)
68    }
69}
70
71/// Enum representing various errors encountered by the library.
72#[derive(Debug)]
73pub enum Error {
74    /// IO error that occurred during file operations,
75    /// network connections, or any other type of I/O operation.
76    IO(io::Error),
77
78    /// Error encountered while parsing data using the library's functions.
79    Parse(ParseErr),
80
81    /// Timeout error, indicating that an operation timed out
82    /// after waiting for the specified duration.
83    Timeout,
84
85    /// Error encountered while using TLS/SSL cryptographic protocols,
86    /// such as establishing secure connections with servers.
87    Tls,
88
89    /// Thread-related communication error, signifying an issue
90    /// that occurred during inter-thread communication.
91    Thread,
92}
93
94impl error::Error for Error {
95    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
96        use self::Error::*;
97
98        match self {
99            IO(e) => Some(e),
100            Parse(e) => Some(e),
101            _ => None,
102        }
103    }
104}
105
106impl fmt::Display for Error {
107    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108        use self::Error::*;
109
110        let err = match self {
111            IO(e) => &format!("IO Error - {}", e),
112            Parse(e) => return e.fmt(f),
113            Timeout => "Timeout error",
114            Tls => "TLS error",
115            Thread => "Thread communication error",
116        };
117        write!(f, "Error: {}", err)
118    }
119}
120
121impl From<io::Error> for Error {
122    fn from(e: io::Error) -> Self {
123        Error::IO(e)
124    }
125}
126
127impl From<ParseErr> for Error {
128    fn from(e: ParseErr) -> Self {
129        Error::Parse(e)
130    }
131}
132
133impl From<str::Utf8Error> for Error {
134    fn from(e: str::Utf8Error) -> Self {
135        Error::Parse(ParseErr::Utf8(e))
136    }
137}
138
139impl From<mpsc::RecvTimeoutError> for Error {
140    fn from(_e: mpsc::RecvTimeoutError) -> Self {
141        Error::Timeout
142    }
143}
144
145#[cfg(feature = "rust-tls")]
146impl From<rustls::Error> for Error {
147    fn from(_e: rustls::Error) -> Self {
148        Error::Tls
149    }
150}
151
152#[cfg(feature = "native-tls")]
153impl From<native_tls::Error> for Error {
154    fn from(_e: native_tls::Error) -> Self {
155        Error::Tls
156    }
157}
158
159#[cfg(feature = "native-tls")]
160impl<T> From<native_tls::HandshakeError<T>> for Error {
161    fn from(_e: native_tls::HandshakeError<T>) -> Self {
162        Error::Tls
163    }
164}
165
166impl<T> From<mpsc::SendError<T>> for Error {
167    fn from(_e: mpsc::SendError<T>) -> Self {
168        Error::Thread
169    }
170}