zoho_crm/
client_error.rs

1use crate::response::ApiErrorResponse;
2use std::error;
3use std::fmt;
4
5/// Various errors returned by the API.
6#[derive(Debug)]
7pub enum ClientError {
8    /// General error message that encompasses almost any non-token related error message.
9    General(String),
10
11    /// Error returned when a response from the API does not deserialize into the user's
12    /// custom data type. The raw response will be returned with this error.
13    UnexpectedResponseType(String),
14
15    /// Error returned from most API requests.
16    ApiError(ApiErrorResponse),
17}
18
19impl ClientError {
20    /// Return the underlying error message as as string.
21    pub fn to_string(&self) -> String {
22        match self {
23            ClientError::General(error) => error.clone(),
24            ClientError::UnexpectedResponseType(error) => error.clone(),
25            ClientError::ApiError(error) => error.to_string(),
26        }
27    }
28}
29
30impl error::Error for ClientError {}
31
32impl fmt::Display for ClientError {
33    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        match self {
35            ClientError::General(error) => write!(f, "{}", error),
36            ClientError::UnexpectedResponseType(error) => write!(f, "{}", error),
37            ClientError::ApiError(error) => write!(f, "{}", error.to_string()),
38        }
39    }
40}
41
42impl From<String> for ClientError {
43    fn from(err: String) -> ClientError {
44        ClientError::General(err)
45    }
46}
47
48impl From<serde_json::Error> for ClientError {
49    fn from(err: serde_json::Error) -> Self {
50        ClientError::General(err.to_string())
51    }
52}
53
54impl From<serde_urlencoded::ser::Error> for ClientError {
55    fn from(err: serde_urlencoded::ser::Error) -> Self {
56        ClientError::General(err.to_string())
57    }
58}
59
60impl From<&str> for ClientError {
61    fn from(err: &str) -> ClientError {
62        ClientError::General(String::from(err))
63    }
64}
65
66impl From<reqwest::Error> for ClientError {
67    fn from(err: reqwest::Error) -> ClientError {
68        ClientError::General(err.to_string())
69    }
70}