From 3f5c1db4305dc20f5839127ad422892874b7836b Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Tue, 21 Aug 2018 17:02:59 -0400 Subject: [PATCH] Breakout errors into their own module --- src/errors.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 87 +++++--------------------------------------------- 2 files changed, 96 insertions(+), 79 deletions(-) create mode 100644 src/errors.rs diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..0043c74 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,88 @@ +use std::{ + io, + fmt, + error +}; + +use json::Error as SerdeError; +use reqwest::Error as HttpError; +use reqwest::StatusCode; +use url::ParseError as UrlError; + +/// Convience type over `std::result::Result` with `Error` as the error type. +pub type Result = ::std::result::Result; + +/// enum of possible errors encountered using the mastodon API. +#[derive(Debug, Deserialize)] +#[serde(untagged)] +pub enum Error { + /// Error from the Mastodon API. This typically means something went + /// wrong with your authentication or data. + Api(ApiError), + /// Error deserialising to json. Typically represents a breaking change in + /// the Mastodon API + #[serde(skip_deserializing)] + Serde(SerdeError), + /// Error encountered in the HTTP backend while requesting a route. + #[serde(skip_deserializing)] + Http(HttpError), + /// Wrapper around the `std::io::Error` struct. + #[serde(skip_deserializing)] + Io(io::Error), + /// Wrapper around the `url::ParseError` struct. + #[serde(skip_deserializing)] + Url(UrlError), + /// Missing Client Id. + #[serde(skip_deserializing)] + ClientIdRequired, + /// Missing Client Secret. + #[serde(skip_deserializing)] + ClientSecretRequired, + /// Missing Access Token. + #[serde(skip_deserializing)] + AccessTokenRequired, + /// Generic client error. + #[serde(skip_deserializing)] + Client(StatusCode), + /// Generic server error. + #[serde(skip_deserializing)] + Server(StatusCode), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::Api(ref e) => { + e.error_description.as_ref().map(|i| &**i) + .or(e.error.as_ref().map(|i| &**i)) + .unwrap_or("Unknown API Error") + }, + Error::Serde(ref e) => e.description(), + Error::Http(ref e) => e.description(), + Error::Io(ref e) => e.description(), + Error::Url(ref e) => e.description(), + Error::Client(ref status) | Error::Server(ref status) => { + status.canonical_reason().unwrap_or("Unknown Status code") + }, + Error::ClientIdRequired => "ClientIdRequired", + Error::ClientSecretRequired => "ClientSecretRequired", + Error::AccessTokenRequired => "AccessTokenRequired", + } + } +} + +/// Error returned from the Mastodon API. +#[derive(Clone, Debug, Deserialize)] +pub struct ApiError { + /// The type of error. + pub error: Option, + /// The description of the error. + pub error_description: Option, +} + diff --git a/src/lib.rs b/src/lib.rs index e5f11d0..0fb1126 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,26 +53,29 @@ pub mod entities; pub mod registration; /// Handling multiple pages of entities. pub mod page; +/// Errors +pub mod errors; + +pub mod prelude { + pub use {Mastodon, MastodonClient, StatusBuilder, StatusesRequest}; +} use std::borrow::Cow; -use std::error::Error as StdError; -use std::fmt; use std::io::Error as IoError; use std::ops; use json::Error as SerdeError; use reqwest::Error as HttpError; -use reqwest::{Client, Response, StatusCode}; +use reqwest::{Client, Response}; use reqwest::header::{Authorization, Bearer, Headers}; use url::ParseError as UrlError; use entities::prelude::*; pub use status_builder::StatusBuilder; use page::Page; +pub use errors::{Result, Error, ApiError}; pub use registration::Registration; -/// Convience type over `std::result::Result` with `Error` as the error type. -pub type Result = std::result::Result; macro_rules! methods { ($($method:ident,)+) => { @@ -268,80 +271,6 @@ pub struct Data { pub token: Cow<'static, str>, } -/// enum of possible errors encountered using the mastodon API. -#[derive(Debug, Deserialize)] -#[serde(untagged)] -pub enum Error { - /// Error from the Mastodon API. This typically means something went - /// wrong with your authentication or data. - Api(ApiError), - /// Error deserialising to json. Typically represents a breaking change in - /// the Mastodon API - #[serde(skip_deserializing)] - Serde(SerdeError), - /// Error encountered in the HTTP backend while requesting a route. - #[serde(skip_deserializing)] - Http(HttpError), - /// Wrapper around the `std::io::Error` struct. - #[serde(skip_deserializing)] - Io(IoError), - /// Wrapper around the `url::ParseError` struct. - #[serde(skip_deserializing)] - Url(UrlError), - /// Missing Client Id. - #[serde(skip_deserializing)] - ClientIdRequired, - /// Missing Client Secret. - #[serde(skip_deserializing)] - ClientSecretRequired, - /// Missing Access Token. - #[serde(skip_deserializing)] - AccessTokenRequired, - /// Generic client error. - #[serde(skip_deserializing)] - Client(StatusCode), - /// Generic server error. - #[serde(skip_deserializing)] - Server(StatusCode), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) - } -} - -impl StdError for Error { - fn description(&self) -> &str { - match *self { - Error::Api(ref e) => { - e.error_description.as_ref().map(|i| &**i) - .or(e.error.as_ref().map(|i| &**i)) - .unwrap_or("Unknown API Error") - }, - Error::Serde(ref e) => e.description(), - Error::Http(ref e) => e.description(), - Error::Io(ref e) => e.description(), - Error::Url(ref e) => e.description(), - Error::Client(ref status) | Error::Server(ref status) => { - status.canonical_reason().unwrap_or("Unknown Status code") - }, - Error::ClientIdRequired => "ClientIdRequired", - Error::ClientSecretRequired => "ClientSecretRequired", - Error::AccessTokenRequired => "AccessTokenRequired", - } - } -} - -/// Error returned from the Mastodon API. -#[derive(Clone, Debug, Deserialize)] -pub struct ApiError { - /// The type of error. - pub error: Option, - /// The description of the error. - pub error_description: Option, -} - /// # Example /// /// ```