rusty_box/auth/
auth_errors.rs

1use std::fmt;
2
3#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
4pub struct AuthErrorResponse {
5    /// The type of the error returned.
6    #[serde(rename = "error", skip_serializing_if = "Option::is_none")]
7    pub error: Option<String>,
8    /// The type of the error returned.
9    #[serde(rename = "error_description", skip_serializing_if = "Option::is_none")]
10    pub error_description: Option<String>,
11}
12
13impl AuthErrorResponse {
14    /// An OAuth 2.0 error
15    pub fn new() -> AuthErrorResponse {
16        AuthErrorResponse {
17            error: None,
18            error_description: None,
19        }
20    }
21}
22
23impl fmt::Display for AuthErrorResponse {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        write!(
26            f,
27            "AuthErrorMessage: {:?} ({:?})",
28            self.error, self.error_description,
29        )
30    }
31}
32
33/// Box API Auth errors
34// #[derive(Debug)]
35pub enum AuthError {
36    Network(reqwest::Error),
37    Serde(serde_json::Error),
38    Io(std::io::Error),
39    Generic(String),
40    ResponseError(AuthErrorResponse),
41    JoseError(josekit::JoseError),
42    OpenSSl(openssl::error::ErrorStack),
43}
44
45impl fmt::Display for AuthError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        let (module, e) = match self {
48            AuthError::Network(e) => ("reqwest", e.to_string()),
49            AuthError::Serde(e) => ("serde", e.to_string()),
50            AuthError::Io(e) => ("IO", e.to_string()),
51            AuthError::Generic(e) => ("Token", e.to_string()),
52            AuthError::ResponseError(e) => ("API Error", e.to_string()),
53            AuthError::JoseError(e) => ("Jose", e.to_string()),
54            AuthError::OpenSSl(e) => ("OpenSSL", e.to_string()),
55        };
56        write!(f, "error in {}: {}", module, e)
57    }
58}
59
60impl fmt::Debug for AuthError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        let (module, e) = match self {
63            AuthError::Network(e) => ("reqwest", e.to_string()),
64            AuthError::Serde(e) => ("serde", e.to_string()),
65            AuthError::Io(e) => ("IO", e.to_string()),
66            AuthError::Generic(e) => ("Token", e.to_string()),
67            AuthError::ResponseError(e) => ("API Error", e.to_string()),
68            AuthError::JoseError(e) => ("Jose", e.to_string()),
69            AuthError::OpenSSl(e) => ("OpenSSL", e.to_string()),
70        };
71        write!(f, "error in {}: {}", module, e)
72    }
73}
74
75impl From<reqwest::Error> for AuthError {
76    fn from(e: reqwest::Error) -> Self {
77        AuthError::Network(e)
78    }
79}
80
81impl From<serde_json::Error> for AuthError {
82    fn from(e: serde_json::Error) -> Self {
83        AuthError::Serde(e)
84    }
85}
86
87impl From<std::io::Error> for AuthError {
88    fn from(e: std::io::Error) -> Self {
89        AuthError::Io(e)
90    }
91}
92
93impl From<String> for AuthError {
94    fn from(e: String) -> Self {
95        AuthError::Generic(e)
96    }
97}
98
99impl From<AuthErrorResponse> for AuthError {
100    fn from(e: AuthErrorResponse) -> Self {
101        AuthError::ResponseError(e)
102    }
103}
104
105impl From<josekit::JoseError> for AuthError {
106    fn from(e: josekit::JoseError) -> Self {
107        AuthError::JoseError(e)
108    }
109}
110
111impl From<openssl::error::ErrorStack> for AuthError {
112    fn from(e: openssl::error::ErrorStack) -> Self {
113        AuthError::OpenSSl(e)
114    }
115}