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
extern crate hyper;
extern crate serde_json as json;
extern crate serde_urlencoded as urlencoded;
use std::error::Error as StandardError;
use std::fmt;
use std::io;
#[derive(Debug)]
pub enum Error {
BadStatus { code: u16, body: String },
Decode(json::Error),
Encode(urlencoded::ser::Error),
Http(hyper::Error),
Io(io::Error),
}
#[derive(Debug)]
pub enum Blame {
Client,
Server,
}
impl Error {
pub fn from_status(code: u16, body: String) -> Error {
Error::BadStatus{code: code, body: body}
}
pub fn from_encode(err: urlencoded::ser::Error) -> Error {
Error::Encode(err)
}
pub fn from_decode(err: json::Error) -> Error {
Error::Decode(err)
}
pub fn from_http(err: hyper::Error) -> Error {
Error::Http(err)
}
pub fn from_io(err: io::Error) -> Error {
Error::Io(err)
}
pub fn blame(&self) -> Blame {
match self {
&Error::BadStatus { code, .. } => match code {
400...499 => Blame::Client,
500...599 => Blame::Server,
_ => Blame::Client,
},
&Error::Decode(_) => Blame::Client,
&Error::Encode(_) => Blame::Client,
&Error::Http(_) => Blame::Server,
&Error::Io(_) => Blame::Server,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let message = match self {
&Error::BadStatus { code, ref body } => format!("stripe: bad http status {}: {}", code, body),
&Error::Decode(ref err) => format!("stripe: {}", err),
&Error::Encode(ref err) => format!("stripe: {}", err),
&Error::Http(ref err) => format!("stripe: {}", err),
&Error::Io(ref err) => format!("stripe: {}", err),
};
write!(f, "{}", message)
}
}
impl StandardError for Error {
fn description(&self) -> &str {
match self {
&Error::BadStatus { .. } => "bad http status",
&Error::Decode(ref err) => err.description(),
&Error::Encode(ref err) => err.description(),
&Error::Http(ref err) => err.description(),
&Error::Io(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&StandardError> {
match self {
&Error::BadStatus { .. } => None,
&Error::Decode(ref err) => Some(err),
&Error::Encode(ref err) => Some(err),
&Error::Http(ref err) => Some(err),
&Error::Io(ref err) => Some(err),
}
}
}