diff options
Diffstat (limited to 'src/api/mod.rs')
-rw-r--r-- | src/api/mod.rs | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs index c33e6ba..27f0da1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -18,32 +18,49 @@ use image::DynamicImage; use itertools::Itertools; use reqwest::{Client, StatusCode, Url}; use serde::{de::DeserializeOwned, Serialize}; -use std::path::Path; +use std::{error::Error, fmt, path::Path}; use super::cache::{Cache, CacheError}; /// The base URL of the official Guild Wars 2 API. const BASE_URL: &str = "https://api.guildwars2.com/v2/"; -quick_error! { - #[derive(Debug)] - pub enum ApiError { - ItemNotFound {} - SerializationError(err: serde_json::Error) { - cause(err) - from() - } - CacheError(err: CacheError) { - cause(err) - from() - } - HttpError(err: reqwest::Error) { - cause(err) - from() +#[derive(Debug)] +pub enum ApiError { + ItemNotFound, + SerializationError(serde_json::Error), + CacheError(CacheError), + HttpError(reqwest::Error), + ImageError(image::ImageError), +} + +error_froms! { ApiError, + err: serde_json::Error => ApiError::SerializationError(err), + err: CacheError => ApiError::CacheError(err), + err: reqwest::Error => ApiError::HttpError(err), + err: image::ImageError => ApiError::ImageError(err), +} + +impl fmt::Display for ApiError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + ApiError::ItemNotFound => write!(f, "the requested item was not found in the API"), + ApiError::SerializationError(_) => write!(f, "error deserializing the returned value"), + ApiError::CacheError(_) => write!(f, "error accessing the cache"), + ApiError::HttpError(_) => write!(f, "HTTP error"), + ApiError::ImageError(_) => write!(f, "image processing error"), } - ImageError(err: image::ImageError) { - cause(err) - from() + } +} + +impl Error for ApiError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match *self { + ApiError::SerializationError(ref err) => Some(err), + ApiError::CacheError(ref err) => Some(err), + ApiError::HttpError(ref err) => Some(err), + ApiError::ImageError(ref err) => Some(err), + _ => None, } } } |