use std::error::Error as StdError;
use std::fmt;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
Client(reqwest::Error),
ApiError(ApiError),
EmptySeriesSelector,
UrlParse(url::ParseError),
ResponseParse(serde_json::Error),
MissingField(MissingFieldError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Client(e) => e.fmt(f),
Self::ApiError(e) => e.fmt(f),
Self::EmptySeriesSelector => write!(f, "at least one series selector must be provided in order to query the series endpoint"),
Self::UrlParse(e) => e.fmt(f),
Self::ResponseParse(e) => e.fmt(f),
Self::MissingField(e) => e.fmt(f),
}
}
}
impl StdError for Error {}
#[derive(Debug, Clone, PartialEq)]
pub struct ApiError {
pub(crate) kind: String,
pub(crate) message: String,
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"the API returned error of type {} as part of JSON response: {}",
self.kind, self.message
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MissingFieldError(pub(crate) &'static str);
impl fmt::Display for MissingFieldError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let MissingFieldError(field) = self;
write!(
f,
"expected field '{}' is missing from the JSON payload",
field
)
}
}