0.5 Added API Error handling

master
Aaron Power 7 years ago
parent 5a06265e87
commit 2dd0c83b55
  1. 2
      Cargo.toml
  2. 20
      src/lib.rs

@ -1,6 +1,6 @@
[package] [package]
name = "mammut" name = "mammut"
version = "0.4.1" version = "0.5.0"
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,10 +68,16 @@ macro_rules! methods {
fn $method<T: serde::Deserialize>(&self, url: String) fn $method<T: serde::Deserialize>(&self, url: String)
-> Result<T> -> Result<T>
{ {
Ok(self.client.$method(&url) let result: std::result::Result<T, ApiError> =
self.client.$method(&url)
.headers(self.headers.clone()) .headers(self.headers.clone())
.send()? .send()?
.json()?) .json()?;
match result {
Ok(t) => Ok(t),
Err(error) => Err(Error::Api(error)),
}
} }
)+ )+
}; };
@ -159,6 +165,7 @@ pub struct Data {
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Api(ApiError),
Serde(SerdeError), Serde(SerdeError),
Http(HttpError), Http(HttpError),
ClientIdRequired, ClientIdRequired,
@ -166,6 +173,15 @@ pub enum Error {
AccessTokenRequired, 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 { impl Mastodon {
fn from_registration(base: String, fn from_registration(base: String,
client_id: String, client_id: String,

Loading…
Cancel
Save