opendal_core/services/lakefs/
error.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use http::Response;
21use http::StatusCode;
22use serde::Deserialize;
23
24use crate::raw::*;
25use crate::*;
26
27/// LakefsError is the error returned by Lakefs File System.
28#[derive(Default, Deserialize)]
29struct LakefsError {
30    error: String,
31}
32
33impl Debug for LakefsError {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("LakefsError")
36            .field("message", &self.error.replace('\n', " "))
37            .finish()
38    }
39}
40
41pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
42    let (parts, body) = resp.into_parts();
43    let bs = body.to_bytes();
44
45    let (kind, retryable) = match parts.status {
46        StatusCode::NOT_FOUND => (ErrorKind::NotFound, false),
47        StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => (ErrorKind::PermissionDenied, false),
48        StatusCode::PRECONDITION_FAILED => (ErrorKind::ConditionNotMatch, false),
49        StatusCode::INTERNAL_SERVER_ERROR
50        | StatusCode::BAD_GATEWAY
51        | StatusCode::SERVICE_UNAVAILABLE
52        | StatusCode::GATEWAY_TIMEOUT => (ErrorKind::Unexpected, true),
53        _ => (ErrorKind::Unexpected, false),
54    };
55
56    let message = match serde_json::from_slice::<LakefsError>(&bs) {
57        Ok(hf_error) => format!("{:?}", hf_error.error),
58        Err(_) => String::from_utf8_lossy(&bs).into_owned(),
59    };
60
61    let mut err = Error::new(kind, message);
62
63    err = with_error_response_context(err, parts);
64
65    if retryable {
66        err = err.set_temporary();
67    }
68
69    err
70}
71
72#[cfg(test)]
73mod test {
74    use super::*;
75    use crate::raw::new_json_deserialize_error;
76    use crate::types::Result;
77
78    #[test]
79    fn test_parse_error() -> Result<()> {
80        let resp = r#"
81            {
82                "error": "Invalid username or password."
83            }
84            "#;
85        let decoded_response = serde_json::from_slice::<LakefsError>(resp.as_bytes())
86            .map_err(new_json_deserialize_error)?;
87
88        assert_eq!(decoded_response.error, "Invalid username or password.");
89
90        Ok(())
91    }
92}