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