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
//! Error enum with all possible reasons for errors and wrapping Result type.

use reqwest::{
    header::{HeaderName, InvalidHeaderValue as ReqwestInvalidHeaderValue},
    Error as ReqwestError,
};
use serde_json::Error as JsonError;
use snafu::Snafu;
use std::result::Result as StdResult;
use url::ParseError as UrlParseError;

/// A result type to compose a successful value and the library's [`Error`]
/// type.
///
/// [`Error`]: enum.Error.html
pub type Result<T> = StdResult<T, Error>;

/// An error type to compose a singular error enum between various dependencies'
/// errors.
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum Error {
    /// The response body couldn't be chunked as a UTF-8 valid string.
    ChunkingText {
        /// The reason for the error.
        source: ReqwestError,
    },
    /// An error from the `serde_json` crate.
    ///
    /// A potential reason for this is when there is an error deserializing a
    /// JSON response body.
    Deserializing {
        /// The reason for the error.
        source: JsonError,
        /// The payload that couldn't be deserialized.
        text: String,
    },
    /// A header value was invalid.
    InvalidHeaderValue {
        /// The name of the header.
        name: HeaderName,
        /// The reason for the error.
        source: ReqwestInvalidHeaderValue,
        /// The value that couldn't be parsed.
        value: String,
    },
    /// The formatted URL is invalid.
    ///
    /// This is likely a library issue.
    InvalidUrl {
        /// The reason for the error.
        source: UrlParseError,
        /// The URI that couldn't be parsed.
        uri: String,
    },
    /// An error from the `reqwest` crate.
    Request {
        /// The reason for the error.
        source: ReqwestError,
    },
    /// A token is needed for this request but wasn't present.
    TokenMissing,
}

impl From<ReqwestError> for Error {
    fn from(source: ReqwestError) -> Self {
        Self::Request { source }
    }
}