From 2dd0c83b55304f6ecc1ee8c3252a61fee6e6c189 Mon Sep 17 00:00:00 2001 From: Aaron Power Date: Sat, 22 Apr 2017 23:29:30 +0100 Subject: [PATCH] 0.5 Added API Error handling --- Cargo.toml | 2 +- src/lib.rs | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7318538..f029c60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mammut" -version = "0.4.1" +version = "0.5.0" description = "A wrapper around the Mastodon API." authors = ["Aaron Power "] license = "MIT/Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 1bad27e..d3d8708 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,10 +68,16 @@ macro_rules! methods { fn $method(&self, url: String) -> Result { - Ok(self.client.$method(&url) + let result: std::result::Result = + self.client.$method(&url) .headers(self.headers.clone()) .send()? - .json()?) + .json()?; + + match result { + Ok(t) => Ok(t), + Err(error) => Err(Error::Api(error)), + } } )+ }; @@ -159,6 +165,7 @@ pub struct Data { #[derive(Debug)] pub enum Error { + Api(ApiError), Serde(SerdeError), Http(HttpError), ClientIdRequired, @@ -166,6 +173,15 @@ pub enum Error { AccessTokenRequired, } +/// Error returned from the Mastodon API. +#[derive(Clone, Debug, Deserialize)] +pub struct ApiError { + /// The type of error. + pub error: String, + /// The description of the error. + pub error_description: String, +} + impl Mastodon { fn from_registration(base: String, client_id: String,