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
use http;
use hyper::error::UriError;
use hyper;
use reqwest;
use serde_json;
use std::error::Error as StdError;
use std::fmt;
use super::StellarError;
use uri;
#[derive(Debug)]
pub enum Error {
BadUri,
BadSSL,
BadResponse(StellarError),
ServerError,
Http(http::Error),
JsonParseError(serde_json::error::Error),
Reqwest(reqwest::Error),
TryFromUri(uri::Error),
#[doc(hidden)]
__Nonexhaustive,
}
pub type Result<T> = ::std::result::Result<T, Error>;
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::BadUri => "An invalid uri was specified when constructing the client",
Error::BadSSL => "Unable to resolve tls",
Error::Http(ref inner) => inner.description(),
Error::Reqwest(ref inner) => inner.description(),
Error::JsonParseError(ref inner) => inner.description(),
Error::BadResponse(ref inner) => inner.description(),
Error::TryFromUri(ref inner) => inner.description(),
Error::ServerError => "An unknown error on the server has occurred",
Error::__Nonexhaustive => unreachable!(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description())
}
}
impl From<UriError> for Error {
fn from(_: UriError) -> Self {
Error::BadUri
}
}
impl From<hyper::Error> for Error {
fn from(_: hyper::Error) -> Self {
Error::BadUri
}
}
impl From<http::Error> for Error {
fn from(inner: http::Error) -> Self {
Error::Http(inner)
}
}
impl From<http::uri::InvalidUri> for Error {
fn from(_: http::uri::InvalidUri) -> Self {
Error::BadUri
}
}
impl From<reqwest::UrlError> for Error {
fn from(_: reqwest::UrlError) -> Self {
Error::BadUri
}
}
impl From<reqwest::Error> for Error {
fn from(inner: reqwest::Error) -> Self {
Error::Reqwest(inner)
}
}
impl From<serde_json::error::Error> for Error {
fn from(inner: serde_json::error::Error) -> Self {
Error::JsonParseError(inner)
}
}
impl From<uri::Error> for Error {
fn from(inner: uri::Error) -> Self {
Error::TryFromUri(inner)
}
}
#[cfg(test)]
mod error_coversion_tests {
use super::*;
use std::str::FromStr;
#[test]
fn it_coerces_an_http_parse_failure() {
let error = http::Uri::from_str("b l a h").unwrap_err();
let error: Error = error.into();
assert_eq!(
error.description(),
"An invalid uri was specified when constructing the client"
);
}
#[test]
fn it_coerces_a_reqwest_parse_error() {
let error = reqwest::Url::from_str("b l a h").unwrap_err();
let error: Error = error.into();
assert_eq!(
error.description(),
"An invalid uri was specified when constructing the client"
);
}
}