opendal/services/cloudflare_kv/
error.rs1use bytes::Buf;
19use http::Response;
20use http::StatusCode;
21use serde_json::de;
22
23use super::backend::CfKvError;
24use super::backend::CfKvResponse;
25use crate::raw::*;
26use crate::*;
27
28pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
30 let (parts, body) = resp.into_parts();
31 let bs = body.to_bytes();
32
33 let (mut kind, mut retryable) = match parts.status {
34 StatusCode::NOT_FOUND => (ErrorKind::NotFound, false),
35 StatusCode::FORBIDDEN => (ErrorKind::PermissionDenied, true),
37 StatusCode::LOCKED => (ErrorKind::Unexpected, true),
39 StatusCode::INTERNAL_SERVER_ERROR
40 | StatusCode::BAD_GATEWAY
41 | StatusCode::SERVICE_UNAVAILABLE
42 | StatusCode::GATEWAY_TIMEOUT => (ErrorKind::Unexpected, true),
43 _ => (ErrorKind::Unexpected, false),
44 };
45
46 let (message, err) = de::from_reader::<_, CfKvResponse>(bs.clone().reader())
47 .map(|err| (format!("{err:?}"), Some(err)))
48 .unwrap_or_else(|_| (String::from_utf8_lossy(&bs).into_owned(), None));
49
50 if let Some(err) = err {
51 (kind, retryable) = parse_cfkv_error_code(err.errors).unwrap_or((kind, retryable));
52 }
53
54 let mut err = Error::new(kind, message);
55
56 err = with_error_response_context(err, parts);
57
58 if retryable {
59 err = err.set_temporary();
60 }
61
62 err
63}
64
65pub(super) fn parse_cfkv_error_code(errors: Vec<CfKvError>) -> Option<(ErrorKind, bool)> {
66 if errors.is_empty() {
67 return None;
68 }
69
70 match errors[0].code {
71 7400 => Some((ErrorKind::Unexpected, false)),
73 7500 => Some((ErrorKind::NotFound, false)),
75 10000 => Some((ErrorKind::PermissionDenied, false)),
77 _ => None,
78 }
79}