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
//! Error and result module
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;

/// A set of errors for use in the client
#[derive(Debug)]
pub enum Error {
    /// An invalid uri was used to construct the client.
    BadUri,
    /// Was unable to resolve ssl configuration
    BadSSL,
    /// Placeholder for errors that come back from the client.
    BadResponse(StellarError),
    /// Server error detected
    ServerError,
    /// The response was from the http library and resulted in an error.
    /// this type does not map down well and currently is just wrapped
    /// generically. See the inner description for details.
    ///
    /// <https://github.com/hyperium/http/issues/188>
    Http(http::Error),
    /// An error occurred while parsing the json
    ///
    /// <https://docs.serde.rs/serde_json/error/struct.Error.html>
    JsonParseError(serde_json::error::Error),
    /// Catch-all for reqwest error handling
    Reqwest(reqwest::Error),
    /// Errors that occur when converting from uri into something else.
    TryFromUri(uri::Error),
    #[doc(hidden)]
    __Nonexhaustive,
}

/// A result including client specific errors.
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"
        );
    }
}