use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IOError;
use crate::response::Error as ResponseError;
use chrono::format::ParseError as TimeParseError;
use reqwest::Error as ReqwestError;
use serde_json::error::Error as JSONError;
use url::ParseError as URLParseError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
JSON(JSONError),
HTTP(ReqwestError),
IO(IOError),
InvalidAPIKey,
ServerError(ResponseError),
NotFound,
InvalidHTTPHeader(reqwest::header::ToStrError),
MissingLastModified,
InvalidDateFormat(TimeParseError),
MissingSeriesFilterKeys,
MissingImage,
MissingSeriesSlug,
InvalidUrl(URLParseError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
match self {
JSON(e) => write!(f, "JSON error: {}", e),
HTTP(e) => write!(f, "HTTP error: {}", e),
IO(e) => write!(f, "IO error: {}", e),
InvalidAPIKey => write!(f, "Invalid API key"),
ServerError(e) => write!(f, "API Server error: {}", e.error),
NotFound => write!(f, "Not found"),
InvalidHTTPHeader(e) => write!(f, "Non-parsable HTTP header: {}", e),
MissingLastModified => write!(f, "Last modified data missing"),
InvalidDateFormat(e) => write!(f, "Invalid date format: {}", e),
MissingSeriesFilterKeys => write!(f, "No series filter keys provided"),
MissingImage => write!(f, "Image data is missing"),
MissingSeriesSlug => write!(f, "Series slug is missing"),
InvalidUrl(e) => write!(f, "Invalid URL: {}", e),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
use Error::*;
match self {
JSON(e) => Some(e),
HTTP(e) => Some(e),
IO(e) => Some(e),
InvalidHTTPHeader(e) => Some(e),
InvalidDateFormat(e) => Some(e),
InvalidUrl(e) => Some(e),
InvalidAPIKey
| NotFound
| ServerError(_)
| MissingLastModified
| MissingSeriesFilterKeys
| MissingImage
| MissingSeriesSlug => None,
}
}
}
impl From<JSONError> for Error {
fn from(e: JSONError) -> Self {
Self::JSON(e)
}
}
impl From<ReqwestError> for Error {
fn from(e: ReqwestError) -> Self {
Self::HTTP(e)
}
}
impl From<IOError> for Error {
fn from(e: IOError) -> Self {
Self::IO(e)
}
}
impl From<reqwest::header::ToStrError> for Error {
fn from(e: reqwest::header::ToStrError) -> Self {
Self::InvalidHTTPHeader(e)
}
}
impl From<TimeParseError> for Error {
fn from(e: TimeParseError) -> Self {
Self::InvalidDateFormat(e)
}
}
impl From<URLParseError> for Error {
fn from(e: URLParseError) -> Self {
Self::InvalidUrl(e)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>();
}
}