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
mod generated;
pub use self::generated::*;
use crate::xml;
use std::borrow::Cow;
use std::convert::Infallible;
use std::fmt;
use std::io::Write;
use std::str::FromStr;
use hyper::StatusCode;
pub type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;
pub type S3Result<T = (), E = S3Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub struct S3Error(Box<Inner>);
#[derive(Debug)]
struct Inner {
code: S3ErrorCode,
message: Option<Cow<'static, str>>,
request_id: Option<String>,
status_code: Option<StatusCode>,
source: Option<StdError>,
}
impl S3Error {
#[must_use]
pub fn new(code: S3ErrorCode) -> Self {
Self(Box::new(Inner {
code,
message: None,
request_id: None,
status_code: None,
source: None,
}))
}
#[must_use]
pub fn with_message(code: S3ErrorCode, msg: impl Into<Cow<'static, str>>) -> Self {
let mut this = Self::new(code);
this.0.message = Some(msg.into());
this
}
#[must_use]
pub fn with_source(code: S3ErrorCode, source: StdError) -> Self {
let mut this = Self::new(code);
this.0.source = Some(source);
this
}
pub fn set_code(&mut self, val: S3ErrorCode) {
self.0.code = val;
}
pub fn set_message(&mut self, val: impl Into<Cow<'static, str>>) {
self.0.message = Some(val.into());
}
pub fn set_request_id(&mut self, val: impl Into<String>) {
self.0.request_id = Some(val.into());
}
pub fn set_source(&mut self, val: StdError) {
self.0.source = Some(val);
}
pub fn set_status_code(&mut self, val: StatusCode) {
self.0.status_code = Some(val);
}
#[must_use]
pub fn code(&self) -> &S3ErrorCode {
&self.0.code
}
#[must_use]
pub fn message(&self) -> Option<&str> {
self.0.message.as_deref()
}
#[must_use]
pub fn request_id(&self) -> Option<&str> {
self.0.request_id.as_deref()
}
#[must_use]
pub fn source(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
self.0.source.as_deref()
}
#[must_use]
pub fn status_code(&self) -> Option<StatusCode> {
self.0.status_code.or_else(|| self.0.code.status_code())
}
#[must_use]
pub fn internal_error<E>(source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::with_source(S3ErrorCode::InternalError, Box::new(source))
}
}
impl xml::Serialize for S3Error {
fn serialize<W: Write>(&self, s: &mut xml::Serializer<W>) -> xml::SerResult {
s.content("Error", self)
}
}
impl xml::SerializeContent for S3Error {
fn serialize_content<W: Write>(&self, s: &mut xml::Serializer<W>) -> xml::SerResult {
s.content("Code", self.0.code.as_str())?;
if let Some(val) = self.0.message.as_deref() {
s.content("Message", val)?;
}
if let Some(val) = self.0.request_id.as_deref() {
s.content("RequestId", val)?;
}
Ok(())
}
}
impl From<S3ErrorCode> for S3Error {
fn from(code: S3ErrorCode) -> Self {
Self::new(code)
}
}
impl From<Infallible> for S3Error {
fn from(value: Infallible) -> Self {
match value {}
}
}
impl fmt::Display for S3Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("S3Error");
d.field("code", &self.0.code);
if let Some(ref message) = self.0.message {
d.field("message", &message);
}
if let Some(ref request_id) = self.0.request_id {
d.field("request_id", &request_id);
}
if let Some(ref status_code) = self.0.status_code {
d.field("status_code", &status_code);
}
if let Some(ref source) = self.0.source {
d.field("source", &source);
}
d.finish_non_exhaustive()
}
}
#[macro_export]
macro_rules! s3_error {
($source:expr, $code:ident) => {{
let mut err = $crate::s3_error!($code);
err.set_source(Box::new($source));
err
}};
($source:expr, $code:ident, $($arg:tt)+) => {{
let mut err = $crate::s3_error!($code, $($arg)+);
err.set_source(Box::new($source));
err
}};
($code:ident) => {
$crate::S3Error::new($crate::S3ErrorCode::$code)
};
($code:ident, $msg:literal) => {
$crate::S3Error::with_message($crate::S3ErrorCode::$code, $msg)
};
($code:ident, $fmt:literal, $($arg:tt)+) => {
$crate::S3Error::with_message($crate::S3ErrorCode::$code, format!($fmt, $($arg)+))
};
}
impl FromStr for S3ErrorCode {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from_bytes(s.as_bytes()).unwrap())
}
}
impl S3ErrorCode {
pub fn as_str(&self) -> &str {
if let Some(s) = self.as_static_str() {
return s;
}
if let Self::Custom(s) = self {
return s;
}
unreachable!()
}
}