opendal/services/azblob/
error.rs1use std::fmt::Debug;
19
20use bytes::Buf;
21use http::Response;
22use http::StatusCode;
23use quick_xml::de;
24use serde::Deserialize;
25
26use crate::raw::*;
27use crate::*;
28
29#[derive(Default, Deserialize)]
31#[serde(default, rename_all = "PascalCase")]
32struct AzblobError {
33 code: String,
34 message: String,
35 query_parameter_name: String,
36 query_parameter_value: String,
37 reason: String,
38}
39
40impl Debug for AzblobError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 let mut de = f.debug_struct("AzblobError");
43 de.field("code", &self.code);
44 de.field("message", &self.message.replace('\n', " "));
46
47 if !self.query_parameter_name.is_empty() {
48 de.field("query_parameter_name", &self.query_parameter_name);
49 }
50 if !self.query_parameter_value.is_empty() {
51 de.field("query_parameter_value", &self.query_parameter_value);
52 }
53 if !self.reason.is_empty() {
54 de.field("reason", &self.reason);
55 }
56
57 de.finish()
58 }
59}
60
61pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
63 let (parts, body) = resp.into_parts();
64 let bs = body.to_bytes();
65
66 let (kind, retryable) = match parts.status {
67 StatusCode::NOT_FOUND => (ErrorKind::NotFound, false),
68 StatusCode::FORBIDDEN => (ErrorKind::PermissionDenied, false),
69 StatusCode::PRECONDITION_FAILED | StatusCode::NOT_MODIFIED | StatusCode::CONFLICT => {
70 (ErrorKind::ConditionNotMatch, false)
71 }
72 StatusCode::INTERNAL_SERVER_ERROR
73 | StatusCode::BAD_GATEWAY
74 | StatusCode::SERVICE_UNAVAILABLE
75 | StatusCode::GATEWAY_TIMEOUT => (ErrorKind::Unexpected, true),
76 _ => (ErrorKind::Unexpected, false),
77 };
78
79 let bs_content = bs.chunk();
80 let mut message = match de::from_reader::<_, AzblobError>(bs_content.reader()) {
81 Ok(azblob_err) => format!("{azblob_err:?}"),
82 Err(_) => String::from_utf8_lossy(&bs).into_owned(),
83 };
84
85 if message.is_empty() {
87 if let Some(v) = parts.headers.get("x-ms-error-code") {
88 if let Ok(code) = v.to_str() {
89 message = format!(
90 "{:?}",
91 AzblobError {
92 code: code.to_string(),
93 ..Default::default()
94 }
95 )
96 }
97 }
98 }
99
100 let mut err = Error::new(kind, &message);
101
102 err = with_error_response_context(err, parts);
103
104 if retryable {
105 err = err.set_temporary();
106 }
107
108 err
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn test_parse_error() {
117 let bs = bytes::Bytes::from(
118 r#"
119<?xml version="1.0" encoding="utf-8"?>
120<Error>
121 <Code>string-value</Code>
122 <Message>string-value</Message>
123</Error>
124"#,
125 );
126
127 let out: AzblobError = de::from_reader(bs.reader()).expect("must success");
128 println!("{out:?}");
129
130 assert_eq!(out.code, "string-value");
131 assert_eq!(out.message, "string-value");
132 }
133
134 #[test]
135 fn test_parse_error_with_reason() {
136 let bs = bytes::Bytes::from(
137 r#"
138<?xml version="1.0" encoding="utf-8"?>
139<Error>
140 <Code>InvalidQueryParameterValue</Code>
141 <Message>Value for one of the query parameters specified in the request URI is invalid.</Message>
142 <QueryParameterName>popreceipt</QueryParameterName>
143 <QueryParameterValue>33537277-6a52-4a2b-b4eb-0f905051827b</QueryParameterValue>
144 <Reason>invalid receipt format</Reason>
145</Error>
146"#,
147 );
148
149 let out: AzblobError = de::from_reader(bs.reader()).expect("must success");
150 println!("{out:?}");
151
152 assert_eq!(out.code, "InvalidQueryParameterValue");
153 assert_eq!(
154 out.message,
155 "Value for one of the query parameters specified in the request URI is invalid."
156 );
157 assert_eq!(out.query_parameter_name, "popreceipt");
158 assert_eq!(
159 out.query_parameter_value,
160 "33537277-6a52-4a2b-b4eb-0f905051827b"
161 );
162 assert_eq!(out.reason, "invalid receipt format");
163 }
164}