#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("transport error: {0}")]
Transport(#[source] crate::generated::client::HttpError),
#[error("Ticker.app responded with HTTP {status}: {body}")]
Api {
status: u16,
body: String,
retry_after: Option<String>,
},
#[error("Ticker.app response did not match the schema: {0}")]
Decode(String),
#[error("incomplete pagination: {0}")]
Pagination(&'static str),
#[error("invalid request: {0}")]
InvalidRequest(&'static str),
}
impl Error {
pub fn is_unauthorized(&self) -> bool {
matches!(
self,
Self::Api {
status: 401 | 403,
..
}
)
}
pub fn is_rate_limited(&self) -> bool {
matches!(self, Self::Api { status: 429, .. })
}
pub fn api_error(&self) -> Option<crate::ErrorDetail> {
let Self::Api { body, .. } = self else {
return None;
};
serde_json::from_str::<crate::ErrorResponse>(body)
.ok()
.map(|response| response.error)
}
}
impl<E: std::fmt::Debug> From<crate::generated::client::ApiOpError<E>> for Error {
fn from(error: crate::generated::client::ApiOpError<E>) -> Self {
use crate::generated::client::ApiOpError;
match error {
ApiOpError::Transport(error) => Self::Transport(error),
ApiOpError::Api(error) => {
if (200..300).contains(&error.status)
&& let Some(reason) = error.parse_error
{
return Self::Decode(reason);
}
Self::Api {
status: error.status,
retry_after: error
.headers
.get("retry-after")
.and_then(|v| v.to_str().ok())
.map(str::to_owned),
body: error.body,
}
}
}
}
}