fixed error handling.

master
Aaron Power 8 years ago
parent 2dd0c83b55
commit d36c861c19
  1. 2
      Cargo.toml
  2. 24
      src/lib.rs

@ -1,6 +1,6 @@
[package] [package]
name = "mammut" name = "mammut"
version = "0.5.0" version = "0.5.1"
description = "A wrapper around the Mastodon API." description = "A wrapper around the Mastodon API."
authors = ["Aaron Power <theaaronepower@gmail.com>"] authors = ["Aaron Power <theaaronepower@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"

@ -68,15 +68,14 @@ macro_rules! methods {
fn $method<T: serde::Deserialize>(&self, url: String) fn $method<T: serde::Deserialize>(&self, url: String)
-> Result<T> -> Result<T>
{ {
let result: std::result::Result<T, ApiError> = let mut response = self.client.$method(&url)
self.client.$method(&url)
.headers(self.headers.clone()) .headers(self.headers.clone())
.send()? .send()?;
.json()?;
match result { if let Ok(t) = response.json::<T>() {
Ok(t) => Ok(t), Ok(t)
Err(error) => Err(Error::Api(error)), } else {
Err(Error::Api(response.json()?))
} }
} }
)+ )+
@ -100,11 +99,16 @@ macro_rules! route {
)* )*
}); });
Ok(self.client.post(&self.route(concat!("/api/v1/", $url))) let mut response = self.client.post(&self.route(concat!("/api/v1/", $url)))
.headers(self.headers.clone()) .headers(self.headers.clone())
.form(&form_data) .form(&form_data)
.send()? .send()?;
.json()?)
if let Ok(t) = response.json::<$ret>() {
Ok(t)
} else {
Err(Error::Api(response.json()?))
}
} }
route!{$($rest)*} route!{$($rest)*}
}; };

Loading…
Cancel
Save