opendal/services/aliyun_drive/
error.rs1use bytes::Buf;
19use http::Response;
20use serde::Deserialize;
21
22use crate::*;
23
24#[derive(Default, Debug, Deserialize)]
25struct AliyunDriveError {
26 code: String,
27 message: String,
28}
29
30pub(super) fn parse_error(res: Response<Buffer>) -> Error {
31 let (parts, body) = res.into_parts();
32 let bs = body.to_bytes();
33 let (code, message) = serde_json::from_reader::<_, AliyunDriveError>(bs.clone().reader())
34 .map(|err| (Some(err.code), err.message))
35 .unwrap_or((None, String::from_utf8_lossy(&bs).into_owned()));
36 let (kind, retryable) = match parts.status.as_u16() {
37 403 => (ErrorKind::PermissionDenied, false),
38 400 => match code {
39 Some(code) if code == "NotFound.File" => (ErrorKind::NotFound, false),
40 Some(code) if code == "AlreadyExist.File" => (ErrorKind::AlreadyExists, false),
41 Some(code) if code == "PreHashMatched" => (ErrorKind::IsSameFile, false),
42 _ => (ErrorKind::Unexpected, false),
43 },
44 409 => (ErrorKind::AlreadyExists, false),
45 429 => match code {
46 Some(code) if code == "TooManyRequests" => (ErrorKind::RateLimited, true),
47 _ => (ErrorKind::Unexpected, false),
48 },
49 _ => (ErrorKind::Unexpected, false),
50 };
51 let mut err = Error::new(kind, message);
52 if retryable {
53 err = err.set_temporary();
54 }
55 err
56}