0.5 Added API Error handling

This commit is contained in:
Aaron Power
2017-04-22 23:29:30 +01:00
parent 5a06265e87
commit 2dd0c83b55
2 changed files with 19 additions and 3 deletions
+18 -2
View File
@@ -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,