aboutsummaryrefslogtreecommitdiff
path: root/src/api/mod.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2019-12-12 02:16:27 +0100
committerDaniel Schadt <kingdread@gmx.de>2019-12-12 02:16:27 +0100
commit3c92e88164db6a94177fb4adeb18c80dffc377e4 (patch)
treec2fdefbe04901df614a9753093d146f424d42a9c /src/api/mod.rs
parenta6afa81d1d9f2dd7d10fe7c0555ae8a8a6d84867 (diff)
downloadkondou-3c92e88164db6a94177fb4adeb18c80dffc377e4.tar.gz
kondou-3c92e88164db6a94177fb4adeb18c80dffc377e4.tar.bz2
kondou-3c92e88164db6a94177fb4adeb18c80dffc377e4.zip
remove quick_error
quick_error used the deprecated Error::cause interface, therefore we want to use our own error enums with proper methods.
Diffstat (limited to 'src/api/mod.rs')
-rw-r--r--src/api/mod.rs55
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,
}
}
}