tinyget/error.rs
1use std::{error, fmt, io, str};
2
3/// Represents an error while sending, receiving, or parsing an HTTP response.
4#[derive(Debug)]
5pub enum Error {
6 /// The response body contains invalid UTF-8, so the `as_str()`
7 /// conversion failed.
8 InvalidUtf8InBody(str::Utf8Error),
9
10 /// Ran into an IO problem while loading the response.
11 IoError(io::Error),
12 /// Couldn't parse the incoming chunk's length while receiving a
13 /// response with the header `Transfer-Encoding: chunked`.
14 MalformedChunkLength,
15 /// Couldn't parse the `Content-Length` header's value as an
16 /// `usize`.
17 MalformedContentLength,
18 /// The response was a redirection, but the `Location` header is
19 /// missing.
20 RedirectLocationMissing,
21 /// The response redirections caused an infinite redirection loop.
22 InfiniteRedirectionLoop,
23 /// Followed
24 /// [`max_redirections`](struct.Request.html#method.with_max_redirections)
25 /// redirections, won't follow any more.
26 TooManyRedirections,
27 /// The response contained invalid UTF-8 where it should be valid
28 /// (eg. headers), so the response cannot interpreted correctly.
29 InvalidUtf8InResponse,
30 /// Tried to send a secure request (ie. the url started with
31 /// `https://`), but the crate's `https` feature was not enabled,
32 /// and as such, a connection cannot be made.
33 HttpsFeatureNotEnabled,
34 /// This is a special error case, one that should never be
35 /// returned! Think of this as a cleaner alternative to calling
36 /// `unreachable!()` inside the library. If you come across this,
37 /// please open an issue, and include the string inside this
38 /// error, as it can be used to locate the problem.
39 Other(&'static str),
40}
41
42impl fmt::Display for Error {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 use Error::*;
45 match self {
46 IoError(err) => write!(f, "{}", err),
47 InvalidUtf8InBody(err) => write!(f, "{}", err),
48
49 MalformedChunkLength => write!(f, "non-usize chunk length with transfer-encoding: chunked"),
50 MalformedContentLength => write!(f, "non-usize content length"),
51 RedirectLocationMissing => write!(f, "redirection location header missing"),
52 InfiniteRedirectionLoop => write!(f, "infinite redirection loop detected"),
53 TooManyRedirections => write!(f, "too many redirections (over the max)"),
54 InvalidUtf8InResponse => write!(f, "response contained invalid utf-8 where valid utf-8 was expected"),
55 HttpsFeatureNotEnabled => write!(f, "request url contains https:// but the https feature is not enabled"),
56 Other(msg) => write!(f, "error in tinyget: please open an issue in the tinyget repo, include the following: '{}'", msg),
57 }
58 }
59}
60
61impl error::Error for Error {
62 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
63 use Error::*;
64 match self {
65 IoError(err) => Some(err),
66 InvalidUtf8InBody(err) => Some(err),
67 _ => None,
68 }
69 }
70}
71
72impl From<io::Error> for Error {
73 fn from(other: io::Error) -> Error {
74 Error::IoError(other)
75 }
76}