Drew Short
6 years ago
2 changed files with 40 additions and 10 deletions
@ -1,28 +1,57 @@ |
|||
use std::error::Error;
|
|||
use std::error;
|
|||
use std::fmt;
|
|||
use std::net;
|
|||
use std::result;
|
|||
|
|||
pub type Result<T> = result::Result<T, Error>;
|
|||
|
|||
#[derive(Debug)]
|
|||
pub enum Error {
|
|||
APIError(APIError),
|
|||
Cloudflare(cloudflare::errors::Error),
|
|||
AddrParseError(net::AddrParseError)
|
|||
}
|
|||
|
|||
impl From<APIError> for Error {
|
|||
fn from(err: APIError) -> Error {
|
|||
Error::APIError(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<cloudflare::errors::Error> for Error {
|
|||
fn from(err: cloudflare::errors::Error) -> Error {
|
|||
Error::Cloudflare(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<net::AddrParseError> for Error {
|
|||
fn from(err: net::AddrParseError) -> Error {
|
|||
Error::AddrParseError(err)
|
|||
}
|
|||
}
|
|||
|
|||
#[derive(Debug)]
|
|||
pub struct APIError {
|
|||
description: String,
|
|||
original_error: Option<Box<Error>>,
|
|||
original_error: Option<Box<error::Error>>,
|
|||
}
|
|||
|
|||
impl APIError {
|
|||
pub fn new(description: &str, original_error: Option<Box<Error>>) -> APIError {
|
|||
APIError {
|
|||
pub fn new(description: &str, original_error: Option<Box<error::Error>>) -> Error {
|
|||
Error::from(APIError {
|
|||
description: String::from(description),
|
|||
original_error,
|
|||
}
|
|||
})
|
|||
}
|
|||
}
|
|||
|
|||
impl Error for APIError {}
|
|||
impl error::Error for APIError {}
|
|||
|
|||
impl fmt::Display for APIError {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|||
match &self.original_error {
|
|||
Some(original_error) => {
|
|||
write!(f, "{}: \"{}\"", self.description, original_error)
|
|||
write!(f, "{}: \"{:?}\"", self.description, original_error)
|
|||
}
|
|||
None => write!(f, "{}", self.description)
|
|||
}
|
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue