use std::fmt;
use reqwest::{Response, StatusCode};
pub type Result<T> = std::result::Result<T, RiotApiError>;
#[derive(Debug)]
pub struct RiotApiError {
reqwest_errors: Vec<reqwest::Error>,
de_error: Option<crate::de::Error>,
retries: u8,
response: Option<Response>,
status_code: Option<StatusCode>,
}
impl RiotApiError {
pub(crate) fn new(
reqwest_errors: Vec<reqwest::Error>,
de_error: Option<crate::de::Error>,
retries: u8,
response: Option<Response>,
status_code: Option<StatusCode>,
) -> Self {
Self {
reqwest_errors,
de_error,
retries,
response,
status_code,
}
}
#[deprecated = "Use `reqwest_errors()` or `de_error()` instead."]
pub fn source_reqwest_error(&self) -> &reqwest::Error {
self.reqwest_errors.last().unwrap()
}
pub fn reqwest_errors(&self) -> &[reqwest::Error] {
&self.reqwest_errors
}
pub fn de_error(&self) -> Option<&crate::de::Error> {
self.de_error.as_ref()
}
pub fn retries(&self) -> u8 {
self.retries
}
pub fn response(&self) -> Option<&Response> {
self.response.as_ref()
}
pub fn take_response(&mut self) -> Option<Response> {
self.response.take()
}
pub fn status_code(&self) -> Option<StatusCode> {
self.status_code
}
}
impl fmt::Display for RiotApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"Riot API request failed after {} retries with status code {:?}{}:",
self.retries(),
self.status_code(),
if self.response().is_some() {
" (response not parsed)"
} else {
""
},
)?;
for (i, reqwest_error) in self.reqwest_errors().iter().enumerate() {
writeln!(f, "- Reqwest error {}: {}", i + 1, reqwest_error)?;
}
if let Some(serde_error) = self.de_error() {
writeln!(f, "- Deserialization error: {}", serde_error)?;
}
Ok(())
}
}
impl std::error::Error for RiotApiError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.reqwest_errors()
.last()
.map(|e| e as _)
.or_else(|| self.de_error().map(|e| e as _))
}
}
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs, clippy::large_enum_variant)]
pub enum TryRequestError {
#[error("Not enough capacity available to process the request immediately (based on the capacity overhead requested)")]
NotEnoughCapacity,
#[error(transparent)]
RiotApiError(#[from] RiotApiError),
}
pub type TryRequestResult<T> = std::result::Result<T, TryRequestError>;