1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::response::ApiErrorResponse;
use std::error;
use std::fmt;

/// Various errors returned by the API.
#[derive(Debug)]
pub enum ClientError {
    /// General error message that encompasses almost any non-token related error message.
    General(String),

    /// Error returned when a response from the API does not deserialize into the user's
    /// custom data type. The raw response will be returned with this error.
    UnexpectedResponseType(String),

    /// Error returned from most API requests.
    ApiError(ApiErrorResponse),
}

impl ClientError {
    /// Return the underlying error message as as string.
    pub fn to_string(&self) -> String {
        match self {
            ClientError::General(error) => error.clone(),
            ClientError::UnexpectedResponseType(error) => error.clone(),
            ClientError::ApiError(error) => error.to_string(),
        }
    }
}

impl error::Error for ClientError {}

impl fmt::Display for ClientError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ClientError::General(error) => write!(f, "{}", error),
            ClientError::UnexpectedResponseType(error) => write!(f, "{}", error),
            ClientError::ApiError(error) => write!(f, "{}", error.to_string()),
        }
    }
}

impl From<String> for ClientError {
    fn from(err: String) -> ClientError {
        ClientError::General(err)
    }
}

impl From<serde_json::Error> for ClientError {
    fn from(err: serde_json::Error) -> Self {
        ClientError::General(err.to_string())
    }
}

impl From<serde_urlencoded::ser::Error> for ClientError {
    fn from(err: serde_urlencoded::ser::Error) -> Self {
        ClientError::General(err.to_string())
    }
}

impl From<&str> for ClientError {
    fn from(err: &str) -> ClientError {
        ClientError::General(String::from(err))
    }
}

impl From<reqwest::Error> for ClientError {
    fn from(err: reqwest::Error) -> ClientError {
        ClientError::General(err.to_string())
    }
}