2019-10-27 08:34:10 +00:00
|
|
|
use std::error::Error as StdError;
|
2019-10-23 07:39:40 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-10-27 08:34:10 +00:00
|
|
|
/// Re-exported `reqwest` types.
|
|
|
|
pub mod reqwest {
|
2020-03-31 19:49:17 +00:00
|
|
|
pub use reqwest::{
|
2020-04-19 20:08:56 +00:00
|
|
|
Error, Response, StatusCode, Url, header::HeaderMap
|
2020-03-31 19:49:17 +00:00
|
|
|
};
|
2019-10-27 08:34:10 +00:00
|
|
|
}
|
|
|
|
use ::reqwest::*;
|
2019-10-23 07:39:40 +00:00
|
|
|
|
|
|
|
/// Result containing RiotApiError on failure.
|
|
|
|
pub type Result<T> = std::result::Result<T, RiotApiError>;
|
|
|
|
|
|
|
|
/// An error that occurred while processing a Riot API request.
|
|
|
|
///
|
|
|
|
/// Although Riven may make multiple requests due to retries, this will always
|
2019-10-27 08:34:10 +00:00
|
|
|
/// contain exactly one reqwest::Error for the final request which failed.
|
2019-10-23 07:39:40 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RiotApiError {
|
2019-10-27 08:34:10 +00:00
|
|
|
reqwest_error: Error,
|
2019-10-23 07:39:40 +00:00
|
|
|
retries: u8,
|
2020-04-19 20:08:56 +00:00
|
|
|
headers: Option<header::HeaderMap>,
|
2020-03-31 19:49:17 +00:00
|
|
|
status_code: Option<StatusCode>,
|
2020-04-19 20:08:56 +00:00
|
|
|
response_body: Option<String>,
|
2019-10-23 07:39:40 +00:00
|
|
|
}
|
|
|
|
impl RiotApiError {
|
2020-04-19 20:08:56 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
reqwest_error: Error,
|
|
|
|
retries: u8,
|
|
|
|
headers: Option<header::HeaderMap>,
|
|
|
|
status_code: Option<StatusCode>,
|
|
|
|
response_body: Option<String>
|
|
|
|
) -> Self {
|
2019-10-23 07:39:40 +00:00
|
|
|
Self {
|
|
|
|
reqwest_error: reqwest_error,
|
|
|
|
retries: retries,
|
2020-04-19 20:08:56 +00:00
|
|
|
headers: headers,
|
2020-03-31 19:49:17 +00:00
|
|
|
status_code: status_code,
|
2020-04-19 20:08:56 +00:00
|
|
|
response_body: response_body,
|
2019-10-23 07:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-27 08:34:10 +00:00
|
|
|
/// The reqwest::Error for the final failed request.
|
|
|
|
pub fn source_reqwest_error(&self) -> &Error {
|
2019-10-23 07:39:40 +00:00
|
|
|
&self.reqwest_error
|
|
|
|
}
|
|
|
|
/// The number of retires attempted. Zero means exactly one request, zero retries.
|
|
|
|
pub fn retries(&self) -> u8 {
|
|
|
|
self.retries
|
|
|
|
}
|
2020-03-31 19:49:17 +00:00
|
|
|
/// The failed response's HTTP status code.
|
|
|
|
/// `Some(reqwest::StatusCode)` if the request was sent and failed, OR if parsing the response JSON failed.
|
|
|
|
/// `None` if the request was not sent.
|
|
|
|
pub fn status_code(&self) -> Option<StatusCode> {
|
|
|
|
self.status_code
|
|
|
|
}
|
2020-04-19 20:08:56 +00:00
|
|
|
/// The failed response's headers.
|
|
|
|
/// `Some(reqwest::header::HeaderMap)` if the request was sent and failed.
|
|
|
|
/// `None` if the request was not sent.
|
|
|
|
pub fn headers(&self) -> Option<header::HeaderMap> {
|
|
|
|
self.headers.clone()
|
|
|
|
}
|
|
|
|
/// The failed response's body (as a copy).
|
|
|
|
/// `Some(String)` if the request was sent and failed.
|
|
|
|
/// `None` if the request was not sent.
|
|
|
|
pub fn response_body(&self) -> Option<String> {
|
|
|
|
self.response_body.clone()
|
|
|
|
}
|
2019-10-23 07:39:40 +00:00
|
|
|
}
|
|
|
|
impl fmt::Display for RiotApiError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{:#?}", self)
|
|
|
|
}
|
|
|
|
}
|
2019-10-27 08:34:10 +00:00
|
|
|
impl StdError for RiotApiError {
|
|
|
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
2019-10-23 07:39:40 +00:00
|
|
|
Some(&self.reqwest_error)
|
|
|
|
}
|
|
|
|
}
|