1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Errors from TEA HTTP operations.
//!
//! Display text does not include access tokens, API key secrets, or
//! `Authorization` header values.
use libtea_core::{
escape_control_chars, IntegrityOutcome, IntegrityReport, ModelError, PolicyError,
};
use thiserror::Error;
/// Failure of a consumer HTTP operation.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ClientError {
/// The URL was rejected by destination policy.
#[error(transparent)]
Policy(#[from] PolicyError),
/// A TEA document or identifier was not valid.
#[error(transparent)]
Model(#[from] ModelError),
/// JSON that could not be decoded.
#[error("JSON parse error: {0}")]
Json(String),
/// A connection or protocol failure.
#[error("transport error: {0}")]
Transport(String),
/// A response exceeded a configured limit.
#[error("response exceeded the configured size limit")]
TooLarge,
/// Redirects exceeded the configured limit.
#[error("too many redirects")]
TooManyRedirects,
/// A redirect had no usable `Location`.
#[error("redirect location is missing or invalid")]
BadRedirect,
/// The request timed out.
#[error("request timed out")]
Timeout,
/// The server required authentication and no credential was available for that origin.
#[error("authentication is required")]
AuthenticationRequired,
/// The server returned 403. This does not trigger token replacement or failover.
#[error("request is not authorized")]
Forbidden,
/// An HTTP status the operation did not accept.
#[error("TEA API status {status}{}", tea_suffix(tea_error))]
#[non_exhaustive]
Api {
/// HTTP status code.
status: u16,
/// TEA `error` value when the body was a TEA error response.
tea_error: Option<String>,
/// Optional human-readable message. Not a programmatic contract.
message: Option<String>,
},
/// `/discovery` returned a TEA 404. This is not evidence that no updates exist.
#[error("identifier was not resolved ({error})")]
#[non_exhaustive]
Unresolved {
/// TEA error code, preserved even when this client does not know it.
error: String,
/// Optional diagnostic message.
message: Option<String>,
},
/// Well-known or API discovery attempts were exhausted.
#[error("discovery failed during the {stage} stage after {attempts} attempts")]
#[non_exhaustive]
DiscoveryFailed {
/// `well-known` or `api`.
stage: &'static str,
/// Attempts counted toward the bound.
attempts: u32,
},
/// Declared checksums did not all match the decoded artifact bytes, or at
/// least one could not be computed.
#[error("artifact integrity was not verified ({outcome:?})")]
#[non_exhaustive]
Integrity {
/// Summary outcome.
outcome: IntegrityOutcome,
/// Per-algorithm detail.
report: IntegrityReport,
},
/// A local file operation of a streaming download failed (for example the
/// destination exists, is a symbolic link, or could not be written).
/// *Experimental.* The text names the operation and the I/O error kind.
#[error("file error: {0}")]
Io(String),
/// The server answered a `GET` with `429` or `503` and the retries (which
/// honor `Retry-After` up to [`crate::Limits::max_retry_after`]) ran out, or
/// the server asked for a longer wait than that limit allows.
#[error(
"the server asked the client to slow down (HTTP {status}{})",
retry_suffix(retry_after_secs)
)]
#[non_exhaustive]
Throttled {
/// `429` or `503`.
status: u16,
/// The wait the server asked for, in seconds, when it said.
retry_after_secs: Option<u64>,
},
/// Offline mode: the response is not in the cache (or is too old), or the
/// request cannot be served from a cache at all.
#[error("offline: the response is not in the cache")]
Offline,
/// The token endpoint rejected the credential exchange.
#[error("token request failed ({error})")]
#[non_exhaustive]
Token {
/// OAuth error code or HTTP status.
error: String,
},
}
fn retry_suffix(seconds: &Option<u64>) -> String {
match seconds {
Some(seconds) => format!(", retry after {seconds}s"),
None => String::new(),
}
}
fn tea_suffix(error: &Option<String>) -> String {
match error {
Some(code) => format!(" ({code})"),
None => String::new(),
}
}
impl From<serde_json::Error> for ClientError {
fn from(value: serde_json::Error) -> Self {
Self::Json(escape_control_chars(&value.to_string()))
}
}
impl ClientError {
pub(crate) fn transport(err: reqwest::Error) -> Self {
if err.is_timeout() {
return Self::Timeout;
}
// Prefer structured removal: reqwest's Display appends
// ` for url (https://...?secret=...)`. Drop that URL object first.
let message = err.without_url().to_string();
if message.to_ascii_lowercase().contains("authorization")
|| message.to_ascii_lowercase().contains("bearer ")
{
return Self::Transport("request failed".into());
}
// Residual free-text URLs are rewritten only when the whole
// whitespace-bounded span can be parsed; otherwise use a generic
// message rather than risk leaving query-value fragments.
Self::Transport(escape_control_chars(
&crate::transport::sanitize_transport_text(&message),
))
}
}