opendal/services/d1/
error.rs1use bytes::Buf;
19use http::Response;
20use http::StatusCode;
21use serde_json::de;
22
23use super::model::D1Error;
24use super::model::D1Response;
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, d1_err) = de::from_reader::<_, D1Response>(bs.clone().reader())
47 .map(|d1_err| (format!("{d1_err:?}"), Some(d1_err)))
48 .unwrap_or_else(|_| (String::from_utf8_lossy(&bs).into_owned(), None));
49
50 if let Some(d1_err) = d1_err {
51 (kind, retryable) = parse_d1_error_code(d1_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 fn parse_d1_error_code(errors: Vec<D1Error>) -> 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}