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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use thiserror::Error;
#[derive(Error, Debug)]
pub enum OciDistributionError {
#[error("Authentication failure: {0}")]
AuthenticationFailure(String),
#[error("Generic error: {0:?}")]
GenericError(Option<String>),
#[error(transparent)]
HeaderValueError(#[from] reqwest::header::ToStrError),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("Received Image Index/Manifest List, but platform_resolver was not defined on the client config. Consider setting platform_resolver")]
ImageIndexParsingNoPlatformResolverError,
#[error("Image manifest not found: {0}")]
ImageManifestNotFoundError(String),
#[error("Incompatible layer media type: {0}")]
IncompatibleLayerMediaTypeError(String),
#[error(transparent)]
JsonError(#[from] serde_json::error::Error),
#[error("Failed to parse manifest as Versioned object: {0}")]
ManifestParsingError(String),
#[error("cannot push a blob without data")]
PushNoDataError,
#[error("cannot push a layer without data")]
PushLayerNoDataError,
#[error("No layers to pull")]
PullNoLayersError,
#[error("Registry error: url {url}, envelope: {envelope}")]
RegistryError {
envelope: OciEnvelope,
url: String,
},
#[error("Registry did not return a digest header")]
RegistryNoDigestError,
#[error("Registry did not return a location header")]
RegistryNoLocationError,
#[error("Failed to decode registry token: {0}")]
RegistryTokenDecodeError(String),
#[error(transparent)]
RequestError(#[from] reqwest::Error),
#[error("Server error: url {url}, code: {code}, message: {message}")]
ServerError {
code: u16,
url: String,
message: String,
},
#[error("OCI distribution spec violation: {0}")]
SpecViolationError(String),
#[error("Not authorized: url {url}")]
UnauthorizedError {
url: String,
},
#[error("Unsupported media type: {0}")]
UnsupportedMediaTypeError(String),
#[error("Unsupported schema version: {0}")]
UnsupportedSchemaVersionError(i32),
#[error("Failed to parse manifest: {0}")]
VersionedParsingError(String),
}
pub type Result<T> = std::result::Result<T, OciDistributionError>;
#[derive(serde::Deserialize, Debug)]
pub struct OciError {
pub code: OciErrorCode,
#[serde(default)]
pub message: String,
#[serde(default)]
pub detail: serde_json::Value,
}
impl std::error::Error for OciError {
fn description(&self) -> &str {
self.message.as_str()
}
}
impl std::fmt::Display for OciError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "OCI API error: {}", self.message.as_str())
}
}
#[derive(serde::Deserialize, Debug)]
pub struct OciEnvelope {
pub(crate) errors: Vec<OciError>,
}
impl std::fmt::Display for OciEnvelope {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let errors: Vec<String> = self.errors.iter().map(|e| e.to_string()).collect();
write!(f, "OCI API errors: [{}]", errors.join("\n"))
}
}
#[derive(serde::Deserialize, Debug, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum OciErrorCode {
BlobUnknown,
BlobUploadInvalid,
BlobUploadUnknown,
DigestInvalid,
ManifestBlobUnknown,
ManifestInvalid,
ManifestUnknown,
ManifestUnverified,
NameInvalid,
NameUnknown,
SizeInvalid,
TagInvalid,
Unauthorized,
Denied,
Unsupported,
Toomanyrequests,
}
#[cfg(test)]
mod test {
use super::*;
const EXAMPLE_ERROR: &str = r#"
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Name":"hello-wasm","Action":"pull"}]}]}
"#;
#[test]
fn test_deserialize() {
let envelope: OciEnvelope =
serde_json::from_str(EXAMPLE_ERROR).expect("parse example error");
let e = &envelope.errors[0];
assert_eq!(OciErrorCode::Unauthorized, e.code);
assert_eq!("authentication required", e.message);
assert_ne!(serde_json::value::Value::Null, e.detail);
}
const EXAMPLE_ERROR_TOOMANYREQUESTS: &str = r#"
{"errors":[{"code":"TOOMANYREQUESTS","message":"pull request limit exceeded","detail":"You have reached your pull rate limit."}]}
"#;
#[test]
fn test_deserialize_toomanyrequests() {
let envelope: OciEnvelope =
serde_json::from_str(EXAMPLE_ERROR_TOOMANYREQUESTS).expect("parse example error");
let e = &envelope.errors[0];
assert_eq!(OciErrorCode::Toomanyrequests, e.code);
assert_eq!("pull request limit exceeded", e.message);
assert_ne!(serde_json::value::Value::Null, e.detail);
}
const EXAMPLE_ERROR_MISSING_MESSAGE: &str = r#"
{"errors":[{"code":"UNAUTHORIZED","detail":[{"Type":"repository","Name":"hello-wasm","Action":"pull"}]}]}
"#;
#[test]
fn test_deserialize_without_message_field() {
let envelope: OciEnvelope =
serde_json::from_str(EXAMPLE_ERROR_MISSING_MESSAGE).expect("parse example error");
let e = &envelope.errors[0];
assert_eq!(OciErrorCode::Unauthorized, e.code);
assert_eq!(String::default(), e.message);
assert_ne!(serde_json::value::Value::Null, e.detail);
}
const EXAMPLE_ERROR_MISSING_DETAIL: &str = r#"
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required"}]}
"#;
#[test]
fn test_deserialize_without_detail_field() {
let envelope: OciEnvelope =
serde_json::from_str(EXAMPLE_ERROR_MISSING_DETAIL).expect("parse example error");
let e = &envelope.errors[0];
assert_eq!(OciErrorCode::Unauthorized, e.code);
assert_eq!("authentication required", e.message);
assert_eq!(serde_json::value::Value::Null, e.detail);
}
}