opendal/services/pcloud/
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 serde::Deserialize;
22
23use crate::raw::*;
24use crate::*;
25
26/// PcloudError is the error returned by Pcloud service.
27#[derive(Default, Deserialize)]
28pub(super) struct PcloudError {
29    pub result: u32,
30    pub error: Option<String>,
31}
32
33impl Debug for PcloudError {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("PcloudError")
36            .field("result", &self.result)
37            .field("error", &self.error)
38            .finish_non_exhaustive()
39    }
40}
41
42/// Parse error response into Error.
43pub(super) fn parse_error(resp: Response<Buffer>) -> Error {
44    let (parts, body) = resp.into_parts();
45    let bs = body.to_bytes();
46    let message = String::from_utf8_lossy(&bs).into_owned();
47
48    let mut err = Error::new(ErrorKind::Unexpected, message);
49
50    err = with_error_response_context(err, parts);
51
52    err
53}
54
55#[cfg(test)]
56mod test {
57    use http::StatusCode;
58
59    use super::*;
60
61    #[tokio::test]
62    async fn test_parse_error() {
63        let err_res = vec![(
64            r#"<html>
65
66                <head>
67                    <title>Invalid link</title>
68                </head>
69
70                <body>This link was generated for another IP address. Try previous step again.</body>
71
72                </html> "#,
73            ErrorKind::Unexpected,
74            StatusCode::GONE,
75        )];
76
77        for res in err_res {
78            let bs = bytes::Bytes::from(res.0);
79            let body = Buffer::from(bs);
80            let resp = Response::builder().status(res.2).body(body).unwrap();
81
82            let err = parse_error(resp);
83
84            assert_eq!(err.kind(), res.1);
85        }
86    }
87}