opendal/services/s3/
error.rs1use bytes::Buf;
19use http::response::Parts;
20use http::Response;
21use quick_xml::de;
22use serde::Deserialize;
23
24use crate::raw::*;
25use crate::*;
26
27#[derive(Default, Debug, Deserialize, PartialEq, Eq)]
29#[serde(default, rename_all = "PascalCase")]
30pub(crate) struct S3Error {
31 pub code: String,
32 pub message: String,
33 pub resource: String,
34 pub request_id: String,
35}
36
37pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
39 let (parts, body) = resp.into_parts();
40 let bs = body.to_bytes();
41
42 let (mut kind, mut retryable) = match parts.status.as_u16() {
43 403 => (ErrorKind::PermissionDenied, false),
44 404 => (ErrorKind::NotFound, false),
45 304 | 412 => (ErrorKind::ConditionNotMatch, false),
46 499 => (ErrorKind::Unexpected, true),
49 500 | 502 | 503 | 504 => (ErrorKind::Unexpected, true),
50 _ => (ErrorKind::Unexpected, false),
51 };
52
53 let body_content = bs.chunk();
54 let (message, s3_err) = de::from_reader::<_, S3Error>(body_content.reader())
55 .map(|s3_err| (format!("{s3_err:?}"), Some(s3_err)))
56 .unwrap_or_else(|_| (String::from_utf8_lossy(&bs).into_owned(), None));
57
58 if let Some(s3_err) = s3_err {
59 (kind, retryable) = parse_s3_error_code(s3_err.code.as_str()).unwrap_or((kind, retryable));
60 }
61
62 let mut err = Error::new(kind, message);
63
64 err = with_error_response_context(err, parts);
65
66 if retryable {
67 err = err.set_temporary();
68 }
69
70 err
71}
72
73pub(crate) fn from_s3_error(s3_error: S3Error, parts: Parts) -> Error {
75 let (kind, retryable) =
76 parse_s3_error_code(s3_error.code.as_str()).unwrap_or((ErrorKind::Unexpected, false));
77 let mut err = Error::new(kind, format!("{s3_error:?}"));
78
79 err = with_error_response_context(err, parts);
80
81 if retryable {
82 err = err.set_temporary();
83 }
84
85 err
86}
87
88pub fn parse_s3_error_code(code: &str) -> Option<(ErrorKind, bool)> {
91 match code {
92 "NoSuchBucket" => Some((ErrorKind::ConfigInvalid, false)),
97 "RequestTimeout" => Some((ErrorKind::Unexpected, true)),
102 "InternalError" => Some((ErrorKind::Unexpected, true)),
104 "OperationAborted" => Some((ErrorKind::Unexpected, true)),
107 "SlowDown" => Some((ErrorKind::RateLimited, true)),
111 "ServiceUnavailable" => Some((ErrorKind::Unexpected, true)),
117 _ => None,
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
127 fn test_parse_error() {
128 let bs = bytes::Bytes::from(
129 r#"
130<?xml version="1.0" encoding="UTF-8"?>
131<Error>
132 <Code>NoSuchKey</Code>
133 <Message>The resource you requested does not exist</Message>
134 <Resource>/mybucket/myfoto.jpg</Resource>
135 <RequestId>4442587FB7D0A2F9</RequestId>
136</Error>
137"#,
138 );
139
140 let out: S3Error = de::from_reader(bs.reader()).expect("must success");
141 println!("{out:?}");
142
143 assert_eq!(out.code, "NoSuchKey");
144 assert_eq!(out.message, "The resource you requested does not exist");
145 assert_eq!(out.resource, "/mybucket/myfoto.jpg");
146 assert_eq!(out.request_id, "4442587FB7D0A2F9");
147 }
148
149 #[test]
150 fn test_parse_error_from_unrelated_input() {
151 let bs = bytes::Bytes::from(
152 r#"
153<?xml version="1.0" encoding="UTF-8"?>
154<CompleteMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
155 <Location>http://Example-Bucket.s3.ap-southeast-1.amazonaws.com/Example-Object</Location>
156 <Bucket>Example-Bucket</Bucket>
157 <Key>Example-Object</Key>
158 <ETag>"3858f62230ac3c915f300c664312c11f-9"</ETag>
159</CompleteMultipartUploadResult>
160"#,
161 );
162
163 let out: S3Error = de::from_reader(bs.reader()).expect("must success");
164 assert_eq!(out, S3Error::default());
165 }
166}