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]
name = "mammut"
version = "0.4.1"
version = "0.5.0"
description = "A wrapper around the Mastodon API."
authors = ["Aaron Power <theaaronepower@gmail.com>"]
license = "MIT/Apache-2.0"

@ -68,10 +68,16 @@ macro_rules! methods {
fn $method<T: serde::Deserialize>(&self, url: String)
-> Result<T>
{
Ok(self.client.$method(&url)
let result: std::result::Result<T, ApiError> =
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,

Loading…
Cancel
Save