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
use std::{self, fmt};
use hyper;
use rustc_serialize;
#[derive(Debug, RustcDecodable, RustcEncodable)]
pub struct TwitterErrors {
pub errors: Vec<TwitterErrorCode>,
}
impl fmt::Display for TwitterErrors {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut first = true;
for e in &self.errors {
if first { first = false; }
else { try!(writeln!(f, ",")); }
try!(write!(f, "{}", e));
}
Ok(())
}
}
#[derive(Debug, RustcDecodable, RustcEncodable)]
pub struct TwitterErrorCode {
pub message: String,
pub code: i32,
}
impl fmt::Display for TwitterErrorCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "#{}: {}", self.code, self.message)
}
}
#[derive(Debug)]
pub enum Error {
InvalidResponse(&'static str, Option<String>),
MissingValue(&'static str),
TwitterError(TwitterErrors),
RateLimit(i32),
BadStatus(hyper::status::StatusCode),
NetError(hyper::error::Error),
IOError(std::io::Error),
JSONError(rustc_serialize::json::ParserError),
DecodeError(rustc_serialize::json::DecoderError),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Error::InvalidResponse(err, ref ext) => write!(f, "Invalid response received: {} ({:?})", err, ext),
Error::MissingValue(val) => write!(f, "Value missing from response: {}", val),
Error::TwitterError(ref err) => write!(f, "Error(s) returned from Twitter: {}", err),
Error::RateLimit(ts) => write!(f, "Rate limit reached, hold until {}", ts),
Error::BadStatus(ref val) => write!(f, "Error status received: {}", val),
Error::NetError(ref err) => write!(f, "Network error: {}", err),
Error::IOError(ref err) => write!(f, "IO error: {}", err),
Error::JSONError(ref err) => write!(f, "JSON parse Error: {}", err),
Error::DecodeError(ref err) => write!(f, "JSON decode error: {}", err),
}
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::InvalidResponse(_, _) => "Invalid response received",
Error::MissingValue(_) => "Value missing from response",
Error::TwitterError(_) => "Error returned from Twitter",
Error::RateLimit(_) => "Rate limit for method reached",
Error::BadStatus(_) => "Response included error code",
Error::NetError(ref err) => err.description(),
Error::IOError(ref err) => err.description(),
Error::JSONError(ref err) => err.description(),
Error::DecodeError(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&std::error::Error> {
match *self {
Error::NetError(ref err) => Some(err),
Error::IOError(ref err) => Some(err),
Error::JSONError(ref err) => Some(err),
Error::DecodeError(ref err) => Some(err),
_ => None,
}
}
}
impl From<hyper::error::Error> for Error {
fn from(err: hyper::error::Error) -> Error {
Error::NetError(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::IOError(err)
}
}
impl From<rustc_serialize::json::ParserError> for Error {
fn from(err: rustc_serialize::json::ParserError) -> Error {
Error::JSONError(err)
}
}
impl From<rustc_serialize::json::DecoderError> for Error {
fn from(err: rustc_serialize::json::DecoderError) -> Error {
Error::DecodeError(err)
}
}