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
use crate::error::OciSpecError;
use derive_builder::Builder;
use getset::Getters;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use thiserror::Error;
pub const ERR_REGISTRY: &str = "distribution: registry returned error";
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
BlobUnknown,
BlobUploadInvalid,
BlobUploadUnknown,
DigestInvalid,
ManifestBlobUnknown,
ManifestInvalid,
ManifestUnknown,
NameInvalid,
NameUnknown,
SizeInvalid,
Unauthorized,
Denied,
Unsupported,
#[serde(rename = "TOOMANYREQUESTS")]
TooManyRequests,
}
#[derive(Builder, Clone, Debug, Deserialize, Eq, Error, Getters, PartialEq, Serialize)]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub")]
pub struct ErrorResponse {
errors: Vec<ErrorInfo>,
}
impl Display for ErrorResponse {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", ERR_REGISTRY)
}
}
impl ErrorResponse {
pub fn detail(&self) -> &[ErrorInfo] {
&self.errors
}
}
#[derive(Builder, Clone, Debug, Deserialize, Eq, Getters, PartialEq, Serialize)]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub")]
pub struct ErrorInfo {
code: ErrorCode,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[builder(default = "None")]
message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", with = "json_string")]
#[builder(default = "None")]
detail: Option<String>,
}
mod json_string {
use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
let opt = Option::<serde_json::Value>::deserialize(deserializer)?;
if let Some(data) = opt {
let data = serde_json::to_string(&data).map_err(Error::custom)?;
return Ok(Some(data));
}
Ok(None)
}
pub fn serialize<S>(target: &Option<String>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::Error;
match target {
Some(data) => {
if let Ok(json_value) = serde_json::Value::from_str(data) {
json_value.serialize(serializer)
} else {
Err(Error::custom("invalid JSON"))
}
}
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::Result;
#[test]
fn error_response_success() -> Result<()> {
let response = ErrorResponseBuilder::default().errors(vec![]).build()?;
assert!(response.detail().is_empty());
assert_eq!(response.to_string(), ERR_REGISTRY);
Ok(())
}
#[test]
fn error_response_failure() {
assert!(ErrorResponseBuilder::default().build().is_err());
}
#[test]
fn error_info_success() -> Result<()> {
let info = ErrorInfoBuilder::default()
.code(ErrorCode::BlobUnknown)
.build()?;
assert_eq!(info.code(), &ErrorCode::BlobUnknown);
assert!(info.message().is_none());
assert!(info.detail().is_none());
Ok(())
}
#[test]
fn error_info_failure() {
assert!(ErrorInfoBuilder::default().build().is_err());
}
#[test]
fn error_info_serialize_success() -> Result<()> {
let error_info = ErrorInfoBuilder::default()
.code(ErrorCode::Unauthorized)
.detail(String::from("{ \"key\": \"value\" }"))
.build()?;
assert!(serde_json::to_string(&error_info).is_ok());
Ok(())
}
#[test]
fn error_info_serialize_failure() -> Result<()> {
let error_info = ErrorInfoBuilder::default()
.code(ErrorCode::Unauthorized)
.detail(String::from("abcd"))
.build()?;
assert!(serde_json::to_string(&error_info).is_err());
Ok(())
}
#[test]
fn error_info_deserialize_success() -> Result<()> {
let error_info_str = r#"
{
"code": "MANIFEST_UNKNOWN",
"message": "manifest tagged by \"lates\" is not found",
"detail": {
"Tag": "lates"
}
}"#;
let error_info: ErrorInfo = serde_json::from_str(&error_info_str)?;
assert_eq!(error_info.detail().as_ref().unwrap(), "{\"Tag\":\"lates\"}");
Ok(())
}
}