use super::StellarError;
use http;
use hyper;
use hyper::error::UriError;
use reqwest;
use serde_json;
use std::error::Error as StdError;
use std::fmt;
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"
);
}
}