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, an error of the request signer, and
18 /// a transport failure or an 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 ///
36 /// A fetch that sets
37 /// [`max_error_body`](crate::FetchRequest::max_error_body) gets the
38 /// headers and the body of the reported response in `response`.
39 #[error("http status {status} for {url}")]
40 HttpStatus {
41 /// The status of the response.
42 status: u16,
43 /// The URL of the hop that answered with the status.
44 url: String,
45 /// The headers and the capped body of the response, if
46 /// [`FetchRequest::max_error_body`](crate::FetchRequest::max_error_body)
47 /// asked for them. `None` otherwise.
48 response: Option<Box<crate::ErrorResponse>>,
49 },
50 /// A redirect chain that reached the limit of
51 /// [`max_redirects`](crate::FetcherOptions::max_redirects).
52 ///
53 /// The response at the end of the chain named another URL to follow. Each
54 /// attempt against one destination counts its own hops, so a repeated
55 /// round counts again from the destination that the route named.
56 #[error("redirect from {url} exceeds the {hops}-redirect limit")]
57 RedirectLimit {
58 /// The URL of the last hop, whose `Location` the limit did not follow.
59 url: String,
60 /// The number of redirects that the attempt followed, which is the
61 /// limit.
62 hops: u32,
63 },
64 /// A response that declared more bytes than the cap of the caller.
65 ///
66 /// If a body grows past the cap while it streams, the read fails with the
67 /// [`FileTooLarge`](std::io::ErrorKind::FileTooLarge) kind. The payload of
68 /// that I/O error is a message string. It does not downcast to [`Error`].
69 #[error("fetched object exceeds the {limit}-byte cap")]
70 FetchTooLarge {
71 /// The cap that the caller set on the request.
72 limit: u64,
73 },
74 /// A response that declared a coding in `Content-Encoding` or
75 /// `Transfer-Encoding`.
76 ///
77 /// The body of such a response holds bytes other than the bytes that the
78 /// remote stores. The value `identity` in each header and the value
79 /// `chunked` in `Transfer-Encoding` declare no coding.
80 #[error("response for {url} carries the coding {encoding}")]
81 ContentEncoded {
82 /// The URL that answered.
83 url: String,
84 /// The coding that the response declared.
85 encoding: String,
86 },
87 /// A URL or a proxy variable that the fetcher cannot use.
88 ///
89 /// The causes are a proxy URL or a proxy variable that the fetcher cannot
90 /// carry. A request URL whose scheme is not `http` or `https` is also a
91 /// cause.
92 /// [`Proxy`](crate::Proxy) states the rules of the proxy URL and of the
93 /// proxy variables.
94 #[error("unsupported: {0}")]
95 Unsupported(String),
96 /// An upload that failed after hyper took its request.
97 ///
98 /// The causes are:
99 ///
100 /// - The connection failed while the request or the response head was in
101 /// transit.
102 /// - The writer of the body was dropped, or a frame of the body waited
103 /// past the stall window.
104 /// - No response head arrived within the response window.
105 /// - The response head is one that the upload does not deliver. Such a
106 /// head names a redirect that the upload cannot follow, or is a 2xx
107 /// head that declares a coding or a length over the cap. An answer
108 /// other than 2xx with such a body keeps its status, and the upload
109 /// withholds the body.
110 ///
111 /// The server can have received the whole request and acted on it, so the
112 /// outcome is unknown. The fetcher does not send the request again.
113 #[error("upload to {url} interrupted: {message}")]
114 UploadInterrupted {
115 /// The URL that the request was sent to.
116 url: String,
117 /// The text that names what ended the upload.
118 message: String,
119 },
120}
121
122impl Error {
123 /// Returns `true` if an upload failed before it handed its request over.
124 ///
125 /// The hand-over gives the request to hyper. No byte of a request
126 /// reaches a server before the hand-over, so the server does nothing for
127 /// it.
128 ///
129 /// Each failure of an upload after the hand-over is
130 /// [`Error::UploadInterrupted`], whatever ended it. The method returns
131 /// `false` for that variant. It returns `true` for each other error of an
132 /// upload. Examples are a refusal before admission, a failed connect or
133 /// handshake, and rounds that ran out before the request was sent.
134 ///
135 /// The answer applies only to the errors of
136 /// [`Fetcher::upload`](crate::Fetcher::upload). A fetch never fails with
137 /// `UploadInterrupted`, and it can fail with any other variant after it
138 /// sent its request.
139 pub fn is_unsent(&self) -> bool {
140 !matches!(self, Error::UploadInterrupted { .. })
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 /// An interrupted upload was sent. Each other error of an upload is a
149 /// failure before the hand-over.
150 #[test]
151 fn an_interrupted_upload_alone_was_sent() {
152 let interrupted = Error::UploadInterrupted {
153 url: "https://h/x".into(),
154 message: "response for https://h/x carries the coding gzip".into(),
155 };
156 assert!(!interrupted.is_unsent());
157 for unsent in [
158 Error::Fetch("connect to h:443 failed".into()),
159 Error::Unsupported("fetch url scheme ftp".into()),
160 Error::HttpStatus {
161 status: 407,
162 url: "https://h/x".into(),
163 response: None,
164 },
165 ] {
166 assert!(unsent.is_unsent(), "{unsent}");
167 }
168 }
169}