rusty_box/client/
client_error.rs

1use std::fmt;
2
3use crate::auth::auth_errors::AuthError;
4
5use super::client_error_model::BoxAPIErrorResponse;
6
7#[derive(Debug, Clone)]
8pub struct ResponseContent<T> {
9    pub status: reqwest::StatusCode,
10    pub content: String,
11    pub entity: Option<T>,
12}
13
14// #[derive(Debug)]
15pub enum BoxAPIError {
16    Network(reqwest::Error),
17    Serde(serde_json::Error),
18    Io(std::io::Error),
19    ResponseError(BoxAPIErrorResponse),
20    AuthError(AuthError),
21}
22
23impl fmt::Display for BoxAPIErrorResponse {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        write!(f, "APIError {{ object_type: {:?}, status: {:?}, code: {:?}, message: {:?}, context_info: {:?}, help_url: {:?}, request_id: {:?} }}", self.object_type, self.status, self.code, self.message, self.context_info, self.help_url, self.request_id)
26    }
27}
28
29impl fmt::Display for BoxAPIError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        let (module, e) = match self {
32            BoxAPIError::Network(e) => ("reqwest", e.to_string()),
33            BoxAPIError::Serde(e) => ("serde", e.to_string()),
34            BoxAPIError::Io(e) => ("IO", e.to_string()),
35            BoxAPIError::ResponseError(e) => ("Box API Error", e.to_string()),
36            BoxAPIError::AuthError(e) => ("Box Auth Error", e.to_string()),
37        };
38        write!(f, "error in {}: {}", module, e)
39    }
40}
41impl fmt::Debug for BoxAPIError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let (module, e) = match self {
44            BoxAPIError::Network(e) => ("reqwest", e.to_string()),
45            BoxAPIError::Serde(e) => ("serde", e.to_string()),
46            BoxAPIError::Io(e) => ("IO", e.to_string()),
47            BoxAPIError::ResponseError(e) => ("Box API Error", e.to_string()),
48            BoxAPIError::AuthError(e) => ("Box Auth Error", e.to_string()),
49        };
50        write!(f, "error in {}: {}", module, e)
51    }
52}
53
54impl From<reqwest::Error> for BoxAPIError {
55    fn from(e: reqwest::Error) -> Self {
56        BoxAPIError::Network(e)
57    }
58}
59
60impl From<serde_json::Error> for BoxAPIError {
61    fn from(e: serde_json::Error) -> Self {
62        BoxAPIError::Serde(e)
63    }
64}
65
66impl From<std::io::Error> for BoxAPIError {
67    fn from(e: std::io::Error) -> Self {
68        BoxAPIError::Io(e)
69    }
70}
71impl From<BoxAPIErrorResponse> for BoxAPIError {
72    fn from(e: BoxAPIErrorResponse) -> Self {
73        BoxAPIError::ResponseError(e)
74    }
75}
76
77impl From<AuthError> for BoxAPIError {
78    fn from(e: AuthError) -> Self {
79        BoxAPIError::AuthError(e)
80    }
81}
82
83// pub fn urlencode<T: AsRef<str>>(s: T) -> String {
84//     ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
85// }
86
87// pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
88//     if let serde_json::Value::Object(object) = value {
89//         let mut params = vec![];
90
91//         for (key, value) in object {
92//             match value {
93//                 serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
94//                     &format!("{}[{}]", prefix, key),
95//                     value,
96//                 )),
97//                 serde_json::Value::Array(array) => {
98//                     for (i, value) in array.iter().enumerate() {
99//                         params.append(&mut parse_deep_object(
100//                             &format!("{}[{}][{}]", prefix, key, i),
101//                             value,
102//                         ));
103//                     }
104//                 }
105//                 serde_json::Value::String(s) => {
106//                     params.push((format!("{}[{}]", prefix, key), s.clone()))
107//                 }
108//                 _ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
109//             }
110//         }
111
112//         return params;
113//     }
114
115//     unimplemented!("Only objects are supported with style=deepObject")
116// }