summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: c291b5dca6f25d47e0ad079744e9862a0a0214f8 (plain)
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
use reqwest::Error as ReqwestError;
use futures::future::SharedError;
use std::convert::From;
use std::sync::Arc;
use serde_json::Error as JsonError;
use crate::models::Message;

#[derive(Clone, Debug)]
pub struct ConditionError {
    inner:  Arc<Error>,
}

impl From<SharedError<ConditionError>> for ConditionError {
    fn from(other: SharedError<ConditionError>) -> Self {
        (*other).clone()
    }
}

impl From<Error> for ConditionError {
    fn from(other: Error) -> Self {
        ConditionError{ inner: Arc::new(other) }
    }
}

#[derive(Debug)]
enum Kind {
    Reqwest(ReqwestError),
    ConditionError(ConditionError),
    Io(std::io::Error),
    Json(JsonError),
    AuthError(Option<Message>),
    RatelimitError(Option<Message>),
}

#[derive(Debug)]
pub struct Error {
    inner: Kind
}

impl Error {
    pub fn auth_error(message: Option<Message>) -> Error {
        Error { inner: Kind::AuthError(message) }
    }

    pub fn ratelimit_error(message: Option<Message>) -> Error {
        Error { inner: Kind::RatelimitError(message) }
    }

    pub fn is_auth_error(&self) -> bool {
        match &self.inner {
            Kind::AuthError(_) => true,
            Kind::ConditionError(condition) => condition.inner.is_auth_error(),
            _ => false,
        }
    }

    pub fn is_ratelimit_error(&self) -> bool {
        match &self.inner {
            Kind::RatelimitError(_) => true,
            Kind::ConditionError(condition) => condition.inner.is_ratelimit_error(),
            _ => false,
        }
    }
}


impl From<reqwest::Error> for Error {

    fn from(err: ReqwestError) -> Error {
        Error {
            inner: Kind::Reqwest(err)
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error { inner: Kind::Io(err) }
    }
}

impl From<JsonError> for Error {

    fn from(err: JsonError) -> Error {
        Error { inner: Kind::Json(err) }
    }
}

impl From<ConditionError> for Error {

    fn from(err: ConditionError) -> Error {
        Error { inner: Kind::ConditionError(err) }
    }
}