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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::{api_error::ApiError, ratelimiting::RatelimitError};
use futures_channel::oneshot::Canceled;
use reqwest::{header::InvalidHeaderValue, Error as ReqwestError, StatusCode};
use std::{
    error::Error as StdError,
    fmt::{Display, Error as FmtError, Formatter, Result as FmtResult},
    num::ParseIntError,
    result::Result as StdResult,
};
use url::ParseError as UrlParseError;

#[cfg(not(feature = "simd-json"))]
use serde_json::Error as JsonError;
#[cfg(feature = "simd-json")]
use simd_json::Error as JsonError;

pub type Result<T, E = Error> = StdResult<T, E>;

#[derive(Debug)]
#[non_exhaustive]
pub enum UrlError {
    UrlParsing { source: UrlParseError },
    IdParsing { source: ParseIntError },
    SegmentMissing,
}

impl Display for UrlError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::UrlParsing { source, .. } => write!(f, "Url path couldn't be parsed: {}", source),
            Self::IdParsing { source, .. } => {
                write!(f, "Url path segment wasn't a valid ID: {}", source)
            }
            Self::SegmentMissing => f.write_str("Url was missing a required path segment"),
        }
    }
}

impl StdError for UrlError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::UrlParsing { source, .. } => Some(source),
            Self::IdParsing { source, .. } => Some(source),
            Self::SegmentMissing => None,
        }
    }
}

impl From<UrlParseError> for UrlError {
    fn from(source: UrlParseError) -> Self {
        Self::UrlParsing { source }
    }
}

impl From<ParseIntError> for UrlError {
    fn from(source: ParseIntError) -> Self {
        Self::IdParsing { source }
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    BuildingClient {
        source: ReqwestError,
    },
    ChunkingResponse {
        source: ReqwestError,
    },
    CreatingHeader {
        name: String,
        source: InvalidHeaderValue,
    },
    Formatting {
        source: FmtError,
    },
    Json {
        source: JsonError,
    },
    Parsing {
        body: Vec<u8>,
        source: JsonError,
    },
    Url {
        source: UrlError,
    },
    Ratelimiting {
        source: RatelimitError,
    },
    RequestCanceled {
        source: Canceled,
    },
    RequestError {
        source: ReqwestError,
    },
    Response {
        body: Vec<u8>,
        error: ApiError,
        status: StatusCode,
    },
}

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

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

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

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::BuildingClient { .. } => {
                f.write_str("HTTP client couldn't be built due to a reqwest client error")
            }
            Self::ChunkingResponse { .. } => f.write_str("Chunking the response failed"),
            Self::CreatingHeader { name, .. } => {
                write!(f, "Parsing the value for header {} failed", name)
            }
            Self::Formatting { .. } => f.write_str("Formatting a string failed"),
            Self::Json { .. } => f.write_str("Given value couldn't be serialized"),
            Self::Parsing { body, .. } => {
                write!(f, "Response body couldn't be deserialized: {:?}", body)
            }
            Self::Url { source, .. } => write!(f, "{}", source),
            Self::Ratelimiting { .. } => f.write_str("Ratelimiting failure"),
            Self::RequestCanceled { .. } => {
                f.write_str("Request was canceled either before or while being sent")
            }
            Self::RequestError { .. } => f.write_str("Parsing or sending the response failed"),
            Self::Response { error, status, .. } => write!(
                f,
                "Response error: status code {}, error: {}",
                status, error
            ),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::CreatingHeader { source, .. } => Some(source),
            Self::Formatting { source } => Some(source),
            Self::Json { source } | Self::Parsing { source, .. } => Some(source),
            Self::Url { source } => Some(source),
            Self::Ratelimiting { source } => Some(source),
            Self::RequestCanceled { source } => Some(source),
            Self::BuildingClient { source }
            | Self::ChunkingResponse { source }
            | Self::RequestError { source } => Some(source),
            Self::Response { .. } => None,
        }
    }
}