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;
|
|
|
|
|
2020-04-21 07:48:57 +00:00
|
|
|
use crate::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,
|
|
|
|
response: Option<Response>,
|
2020-03-31 19:49:17 +00:00
|
|
|
status_code: Option<StatusCode>,
|
2019-10-23 07:39:40 +00:00
|
|
|
}
|
|
|
|
impl RiotApiError {
|
2020-03-31 19:49:17 +00:00
|
|
|
pub(crate) fn new(reqwest_error: Error, retries: u8, response: Option<Response>, status_code: Option<StatusCode>) -> Self {
|
2019-10-23 07:39:40 +00:00
|
|
|
Self {
|
|
|
|
reqwest_error: reqwest_error,
|
|
|
|
retries: retries,
|
|
|
|
response: response,
|
2020-03-31 19:49:17 +00:00
|
|
|
status_code: status_code,
|
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.
|
|
|
|
/// `Some(reqwest::Response)` if the request was sent and failed.
|
|
|
|
/// `None` if the request was not sent, OR if parsing the response JSON failed.
|
|
|
|
pub fn response(&self) -> Option<&Response> {
|
2019-10-23 07:39:40 +00:00
|
|
|
self.response.as_ref()
|
|
|
|
}
|
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
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|