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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::fmt;

#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct AuthErrorResponse {
    /// The type of the error returned.
    #[serde(rename = "error", skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// The type of the error returned.
    #[serde(rename = "error_description", skip_serializing_if = "Option::is_none")]
    pub error_description: Option<String>,
}

impl AuthErrorResponse {
    /// An OAuth 2.0 error
    pub fn new() -> AuthErrorResponse {
        AuthErrorResponse {
            error: None,
            error_description: None,
        }
    }
}

impl fmt::Display for AuthErrorResponse {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "AuthErrorMessage: {:?} ({:?})",
            self.error, self.error_description,
        )
    }
}

/// Box API Auth errors
// #[derive(Debug)]
pub enum AuthError {
    Network(reqwest::Error),
    Serde(serde_json::Error),
    Io(std::io::Error),
    Token(String),
    ResponseError(AuthErrorResponse),
}

impl fmt::Display for AuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (module, e) = match self {
            AuthError::Network(e) => ("reqwest", e.to_string()),
            AuthError::Serde(e) => ("serde", e.to_string()),
            AuthError::Io(e) => ("IO", e.to_string()),
            AuthError::Token(e) => ("Token", e.to_string()),
            AuthError::ResponseError(e) => ("API Error", e.to_string()),
        };
        write!(f, "error in {}: {}", module, e)
    }
}

impl fmt::Debug for AuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (module, e) = match self {
            AuthError::Network(e) => ("reqwest", e.to_string()),
            AuthError::Serde(e) => ("serde", e.to_string()),
            AuthError::Io(e) => ("IO", e.to_string()),
            AuthError::Token(e) => ("Token", e.to_string()),
            AuthError::ResponseError(e) => ("API Error", e.to_string()),
        };
        write!(f, "error in {}: {}", module, e)
    }
}

impl From<reqwest::Error> for AuthError {
    fn from(e: reqwest::Error) -> Self {
        AuthError::Network(e)
    }
}

impl From<serde_json::Error> for AuthError {
    fn from(e: serde_json::Error) -> Self {
        AuthError::Serde(e)
    }
}

impl From<std::io::Error> for AuthError {
    fn from(e: std::io::Error) -> Self {
        AuthError::Io(e)
    }
}

impl From<String> for AuthError {
    fn from(e: String) -> Self {
        AuthError::Token(e)
    }
}

impl From<AuthErrorResponse> for AuthError {
    fn from(e: AuthErrorResponse) -> Self {
        AuthError::ResponseError(e)
    }
}