opendal_core/services/dbfs/
error.rs1use std::fmt::Debug;
19
20use http::Response;
21use http::StatusCode;
22use serde::Deserialize;
23
24use crate::raw::*;
25use crate::*;
26
27#[derive(Default, Deserialize)]
29struct DbfsError {
30 error_code: String,
31 message: String,
32}
33
34impl Debug for DbfsError {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 f.debug_struct("DbfsError")
37 .field("error_code", &self.error_code)
38 .field("message", &self.message.replace('\n', " "))
40 .finish()
41 }
42}
43
44pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
45 let (parts, body) = resp.into_parts();
46 let bs = body.to_bytes();
47
48 let (kind, retryable) = match parts.status {
49 StatusCode::NOT_FOUND => (ErrorKind::NotFound, false),
50 StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => (ErrorKind::PermissionDenied, false),
51 StatusCode::PRECONDITION_FAILED => (ErrorKind::ConditionNotMatch, false),
52 StatusCode::INTERNAL_SERVER_ERROR
53 | StatusCode::BAD_GATEWAY
54 | StatusCode::SERVICE_UNAVAILABLE
55 | StatusCode::GATEWAY_TIMEOUT => (ErrorKind::Unexpected, true),
56 _ => (ErrorKind::Unexpected, false),
57 };
58
59 let message = match serde_json::from_slice::<DbfsError>(&bs) {
60 Ok(dbfs_error) => format!("{:?}", dbfs_error.message),
61 Err(_) => String::from_utf8_lossy(&bs).into_owned(),
62 };
63
64 let mut err = Error::new(kind, message);
65
66 err = with_error_response_context(err, parts);
67
68 if retryable {
69 err = err.set_temporary();
70 }
71
72 err
73}