opendal/services/github/
error.rs1use bytes::Buf;
19use http::Response;
20use serde::Deserialize;
21
22use crate::raw::*;
23use crate::*;
24
25#[derive(Default, Debug, Deserialize)]
26#[allow(dead_code)]
27struct GithubError {
28 error: GithubSubError,
29}
30
31#[derive(Default, Debug, Deserialize)]
32#[allow(dead_code)]
33struct GithubSubError {
34 message: String,
35 documentation_url: String,
36}
37
38pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
40 let (parts, body) = resp.into_parts();
41 let bs = body.to_bytes();
42
43 let (kind, retryable) = match parts.status.as_u16() {
44 401 | 403 => (ErrorKind::PermissionDenied, false),
45 404 => (ErrorKind::NotFound, false),
46 304 | 412 => (ErrorKind::ConditionNotMatch, false),
47 423 => (ErrorKind::RateLimited, true),
51 499 => (ErrorKind::Unexpected, true),
54 500 | 502 | 503 | 504 => (ErrorKind::Unexpected, true),
55 _ => (ErrorKind::Unexpected, false),
56 };
57
58 let (message, _github_content_err) =
59 serde_json::from_reader::<_, GithubError>(bs.clone().reader())
60 .map(|github_content_err| (format!("{github_content_err:?}"), Some(github_content_err)))
61 .unwrap_or_else(|_| (String::from_utf8_lossy(&bs).into_owned(), None));
62
63 let mut err = Error::new(kind, message);
64
65 err = with_error_response_context(err, parts);
66
67 if retryable {
68 err = err.set_temporary();
69 }
70
71 err
72}
73
74#[cfg(test)]
75mod test {
76 use http::StatusCode;
77
78 use super::*;
79
80 #[tokio::test]
81 async fn test_parse_error() {
82 let err_res = vec![(
83 r#"{
84 "message": "Not Found",
85 "documentation_url": "https://docs.github.com/rest/repos/contents#get-repository-content"
86 }"#,
87 ErrorKind::NotFound,
88 StatusCode::NOT_FOUND,
89 )];
90
91 for res in err_res {
92 let bs = bytes::Bytes::from(res.0);
93 let body = Buffer::from(bs);
94 let resp = Response::builder().status(res.2).body(body).unwrap();
95
96 let err = parse_error(resp);
97
98 assert_eq!(err.kind(), res.1);
99 }
100 }
101}