opendal/services/koofr/
error.rs1use http::Response;
19
20use crate::raw::*;
21use crate::*;
22
23pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
25 let (parts, body) = resp.into_parts();
26 let bs = body.to_bytes();
27
28 let (kind, retryable) = match parts.status.as_u16() {
29 403 => (ErrorKind::PermissionDenied, false),
30 404 => (ErrorKind::NotFound, false),
31 304 | 412 => (ErrorKind::ConditionNotMatch, false),
32 499 => (ErrorKind::Unexpected, true),
35 500 | 502 | 503 | 504 => (ErrorKind::Unexpected, true),
36 _ => (ErrorKind::Unexpected, false),
37 };
38
39 let message = String::from_utf8_lossy(&bs).into_owned();
40
41 let mut err = Error::new(kind, message);
42
43 err = with_error_response_context(err, parts);
44
45 if retryable {
46 err = err.set_temporary();
47 }
48
49 err
50}
51
52#[cfg(test)]
53mod test {
54 use http::StatusCode;
55
56 use super::*;
57
58 #[tokio::test]
59 async fn test_parse_error() {
60 let err_res = vec![(r#""#, ErrorKind::NotFound, StatusCode::NOT_FOUND)];
61
62 for res in err_res {
63 let bs = bytes::Bytes::from(res.0);
64 let body = Buffer::from(bs);
65 let resp = Response::builder().status(res.2).body(body).unwrap();
66
67 let err = parse_error(resp);
68
69 assert_eq!(err.kind(), res.1);
70 }
71 }
72}