opendal/services/azdls/
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 AzdlsError {
33 code: String,
34 message: String,
35 query_parameter_name: String,
36 query_parameter_value: String,
37 reason: String,
38}
39
40impl Debug for AzdlsError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 let mut de = f.debug_struct("AzdlsError");
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::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 mut message = match de::from_reader::<_, AzdlsError>(bs.clone().reader()) {
80 Ok(azdls_err) => format!("{azdls_err:?}"),
81 Err(_) => String::from_utf8_lossy(&bs).into_owned(),
82 };
83 if message.is_empty() {
85 if let Some(v) = parts.headers.get("x-ms-error-code") {
86 if let Ok(code) = v.to_str() {
87 message = format!(
88 "{:?}",
89 AzdlsError {
90 code: code.to_string(),
91 ..Default::default()
92 }
93 )
94 }
95 }
96 }
97
98 let mut err = Error::new(kind, &message);
99
100 err = with_error_response_context(err, parts);
101
102 if retryable {
103 err = err.set_temporary();
104 }
105
106 err
107}