authy_rs/
error.rs

1use std::fmt;
2use std::error::Error;
3
4
5
6
7#[derive(Deserialize)]
8#[serde(untagged)]
9pub enum ApiResult<T> {
10    Ok(T),
11    Err(ApiError)
12}
13
14#[derive(Deserialize, Debug)]
15pub struct ApiError {
16    pub code: u32,
17    pub message: String,
18    pub more_info: String,
19    pub status: u32
20}
21
22impl Error for ApiError {
23    fn description(&self) -> &str {
24        self.message.as_str()
25    }
26
27    fn source(&self) -> Option<&(Error + 'static)> {
28        None
29    }
30}
31
32/*
33impl PartialEq for ErrorResponse {
34    fn eq(&self, other: &ErrorResponse) -> bool {
35        self.code == other.code
36    }
37}
38*/
39
40impl fmt::Display for ApiError {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(f, "ErrorResponse #{}: {}", self.code, self.message)
43    }
44}
45
46impl Into<u32> for ApiError {
47    fn into(self) -> u32 {
48        self.code
49    }
50}
51
52
53
54#[derive(Debug)]
55pub enum AuthyErr {
56    Api(ApiError),
57    Http(reqwest::Error)
58}
59
60
61impl Error for AuthyErr {
62    fn description(&self) -> &str {
63        match self {
64            AuthyErr::Api(cause) => cause.description(),
65            AuthyErr::Http(cause) => cause.description(),
66        }
67    }
68
69    fn source(&self) -> Option<&(Error + 'static)> {
70        match self {
71            AuthyErr::Api(cause) => Some(cause),
72            AuthyErr::Http(cause) => Some(cause),
73        }
74    }
75}
76
77impl fmt::Display for AuthyErr {
78    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79        f.write_str(self.description())
80    }
81}