Breakout errors into their own module

master
Paul Woolcock 6 years ago
parent 473205a5f2
commit 3f5c1db430
  1. 88
      src/errors.rs
  2. 87
      src/lib.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<T> = ::std::result::Result<T, Error>;
/// 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<String>,
/// The description of the error.
pub error_description: Option<String>,
}

@ -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<T> = std::result::Result<T, Error>;
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<String>,
/// The description of the error.
pub error_description: Option<String>,
}
/// # Example
///
/// ```

Loading…
Cancel
Save