ostrya_fetch/error.rs
1//! The error type of the fetcher.
2
3/// The result type of this crate, with [`Error`] as the error.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// The error of each fallible operation of this crate.
7///
8/// A caller outside this crate can build each variant. Each variant converts
9/// to the `ostrya::Error` variant of the same name, with the same message.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13 /// A failure to set up or to carry out a request.
14 ///
15 /// The causes include an option value, a mirror URL, a header, or a TLS
16 /// configuration that the fetcher cannot use. They also include a refused
17 /// credential, a refused redirect hop, and a transport failure or an
18 /// expired deadline that outlived the retries.
19 /// [`Fetcher::fetch`](crate::Fetcher::fetch) and
20 /// [`Fetcher::upload`](crate::Fetcher::upload) list the cases.
21 #[error("fetch: {0}")]
22 Fetch(String),
23 /// An HTTP status other than 200 and 304 that ended a fetch.
24 ///
25 /// A 404 means that the remote does not hold the requested file. The
26 /// statuses 408, 429, and 5xx are retryable, and each other status is
27 /// definitive. If a fetch fails, it reports the first definitive failure
28 /// that it received, from any round. If it received no definitive failure,
29 /// it reports the first retryable failure.
30 ///
31 /// If the reported failure is a status, it is this variant.
32 /// [`Fetcher::fetch`](crate::Fetcher::fetch) states the failure order. An
33 /// upload returns each status in an [`Uploaded`](crate::Uploaded) and does
34 /// not fail with this variant.
35 #[error("http status {status} for {url}")]
36 HttpStatus {
37 /// The status of the response.
38 status: u16,
39 /// The URL of the hop that answered with the status.
40 url: String,
41 },
42 /// A redirect chain that reached the limit of
43 /// [`max_redirects`](crate::FetcherOptions::max_redirects).
44 ///
45 /// The response at the end of the chain named another URL to follow. Each
46 /// attempt against one destination counts its own hops, so a repeated
47 /// round counts again from the destination that the route named.
48 #[error("redirect from {url} exceeds the {hops}-redirect limit")]
49 RedirectLimit {
50 /// The URL of the last hop, whose `Location` the limit did not follow.
51 url: String,
52 /// The number of redirects that the attempt followed, which is the
53 /// limit.
54 hops: u32,
55 },
56 /// A response that declared more bytes than the cap of the caller.
57 ///
58 /// If a body grows past the cap while it streams, the read fails with the
59 /// [`FileTooLarge`](std::io::ErrorKind::FileTooLarge) kind. The payload of
60 /// that I/O error is a message string. It does not downcast to [`Error`].
61 #[error("fetched object exceeds the {limit}-byte cap")]
62 FetchTooLarge {
63 /// The cap that the caller set on the request.
64 limit: u64,
65 },
66 /// A response that declared a coding in `Content-Encoding` or
67 /// `Transfer-Encoding`.
68 ///
69 /// The body of such a response holds bytes other than the bytes that the
70 /// remote stores. The value `identity` in each header and the value
71 /// `chunked` in `Transfer-Encoding` declare no coding.
72 #[error("response for {url} carries the coding {encoding}")]
73 ContentEncoded {
74 /// The URL that answered.
75 url: String,
76 /// The coding that the response declared.
77 encoding: String,
78 },
79 /// A URL or a proxy variable that the fetcher cannot use.
80 ///
81 /// The causes are a proxy URL or a proxy variable that the fetcher cannot
82 /// carry. A request URL whose scheme is not `http` or `https` is also a
83 /// cause.
84 /// [`Proxy`](crate::Proxy) states the rules of the proxy URL and of the
85 /// proxy variables.
86 #[error("unsupported: {0}")]
87 Unsupported(String),
88 /// An upload that failed after hyper took its request.
89 ///
90 /// The causes are:
91 ///
92 /// - The connection failed while the request or the response head was in
93 /// transit.
94 /// - The writer of the body was dropped, or a frame of the body waited
95 /// past the stall window.
96 /// - No response head arrived within the response window.
97 /// - The response head is one that the upload does not deliver. Such a
98 /// head declares a coding, declares a length over the cap, or names a
99 /// redirect that the upload cannot follow.
100 ///
101 /// The server can have received the whole request and acted on it, so the
102 /// outcome is unknown. The fetcher does not send the request again.
103 #[error("upload to {url} interrupted: {message}")]
104 UploadInterrupted {
105 /// The URL that the request was sent to.
106 url: String,
107 /// The text that names what ended the upload.
108 message: String,
109 },
110}
111
112impl Error {
113 /// Returns `true` if an upload failed before it handed its request over.
114 ///
115 /// The hand-over gives the request to hyper. No byte of a request
116 /// reaches a server before the hand-over, so the server does nothing for
117 /// it.
118 ///
119 /// Each failure of an upload after the hand-over is
120 /// [`Error::UploadInterrupted`], whatever ended it. The method returns
121 /// `false` for that variant. It returns `true` for each other error of an
122 /// upload. Examples are a refusal before admission, a failed connect or
123 /// handshake, and rounds that ran out before the request was sent.
124 ///
125 /// The answer applies only to the errors of
126 /// [`Fetcher::upload`](crate::Fetcher::upload). A fetch never fails with
127 /// `UploadInterrupted`, and it can fail with any other variant after it
128 /// sent its request.
129 pub fn is_unsent(&self) -> bool {
130 !matches!(self, Error::UploadInterrupted { .. })
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 /// An interrupted upload was sent. Each other error of an upload is a
139 /// failure before the hand-over.
140 #[test]
141 fn an_interrupted_upload_alone_was_sent() {
142 let interrupted = Error::UploadInterrupted {
143 url: "https://h/x".into(),
144 message: "response for https://h/x carries the coding gzip".into(),
145 };
146 assert!(!interrupted.is_unsent());
147 for unsent in [
148 Error::Fetch("connect to h:443 failed".into()),
149 Error::Unsupported("fetch url scheme ftp".into()),
150 Error::HttpStatus {
151 status: 407,
152 url: "https://h/x".into(),
153 },
154 ] {
155 assert!(unsent.is_unsent(), "{unsent}");
156 }
157 }
158}