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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::fmt;
use std::result;
use failure::{Backtrace, Context, Fail};
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
context : Context<ErrType>,
}
impl Error {
pub fn kind(&self) -> &ErrType {
self.context.get_context()
}
pub(crate) fn invalid_url<T: AsRef<str>>(unk: T) -> Error {
Error::from(ErrType::InvalidURL(unk.as_ref().to_string()))
}
pub(crate) fn invalid_path<T: AsRef<str>>(unk: T) -> Error {
Error::from(ErrType::InvalidFile(unk.as_ref().to_string()))
}
pub(crate) fn invalid_serde<T: AsRef<str>>(unk: T) -> Error {
Error::from(ErrType::InvalidSerde(unk.as_ref().to_string()))
}
pub(crate) fn invalid_code(code : i32, message : String) -> Error {
Error::from(ErrType::InvalidCode{code, message})
}
pub(crate) fn invalid_request<T: AsRef<str>>(unk: T) -> Error {
Error::from(ErrType::InvalidRequest(unk.as_ref().to_string()))
}
pub(crate) fn invalid_parameter(message : String) -> Error {
Error::from(ErrType::InvalidParameters(message))
}
}
impl Fail for Error {
fn cause(&self) -> Option<&Fail> {
self.context.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.context.backtrace()
}
}
impl fmt::Display for Error {
fn fmt(&self, f : &mut fmt::Formatter) -> fmt::Result {
self.context.fmt(f)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ErrType {
InvalidURL(String),
InvalidFile(String),
InvalidSerde(String),
InvalidCode {
code : i32,
message : String,
},
InvalidRequest(String),
InvalidParameters(String),
}
impl fmt::Display for ErrType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ErrType::InvalidURL(ref unk) => write!(f, "ERROR: URL was invalid, error was due to: {}", unk),
ErrType::InvalidFile(ref unk) => write!(f, "ERROR: File path was invalid, error was due to: {}", unk),
ErrType::InvalidSerde(ref unk) => write!(f, "ERROR: Could not properly serde results: {}", unk),
ErrType::InvalidCode {code, message} => write!(f, "ERROR: Recieved an invalid status code {} after API call with message: \"{}\"", code, message),
ErrType::InvalidRequest(ref unk) => write!(f, "ERROR: Failed to make the request, error was due to: {}", unk),
ErrType::InvalidParameters(message) => write!(f, "ERROR: An invalid parameter was passed, error was due to: {}", message),
}
}
}
impl From<ErrType> for Error {
fn from(err_type: ErrType) -> Error {
Error::from(Context::new(err_type))
}
}
impl From<Context<ErrType>> for Error {
fn from(context: Context<ErrType>) -> Error {
Error { context }
}
}
impl From<serde_json::Error> for Error {
fn from(err : serde_json::Error) -> Self {
Error::invalid_serde(err.to_string())
}
}
impl From<reqwest::Error> for Error {
fn from(err : reqwest::Error) -> Self {
Error::invalid_request(err.to_string())
}
}
impl From<reqwest::UrlError> for Error {
fn from(err : reqwest::UrlError) -> Self {
Error::invalid_url(err.to_string())
}
}
impl From<std::io::Error> for Error {
fn from(err : std::io::Error) -> Self {
Error::invalid_path(err.to_string())
}
}