1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
use reqwest;
use rpser;
use chrono;
use std::{error as stderror, fmt, io, num};

#[derive(Debug)]
pub enum Error {
    TooManyRecords,
    FnsError(String),
    ReqError(reqwest::Error),
    RpcError(rpser::RpcError),
    XmlError(rpser::xml::Error),
    ParseIntError(num::ParseIntError),
    ParseDateTimeError(chrono::ParseError),
    IoError(io::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::TooManyRecords => write!(
                f,
                "The request can not be more than 10,000 items"
            ),
            Error::FnsError(ref err_msg) => write!(f, "{}", err_msg),
            Error::ReqError(ref e) => fmt::Display::fmt(e, f),
            Error::RpcError(ref e) => fmt::Display::fmt(e, f),
            Error::XmlError(ref e) => fmt::Display::fmt(e, f),
            Error::ParseIntError(ref e) => fmt::Display::fmt(e, f),
            Error::ParseDateTimeError(ref e) => fmt::Display::fmt(e, f),
            Error::IoError(ref e) => fmt::Display::fmt(e, f),
        }
    }
}

impl stderror::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::TooManyRecords => {
                "The request can not be more than 10,000 items"
            }
            Error::FnsError(_) => {
                "The service reported an error processing the request"
            }
            Error::ReqError(ref e) => e.description(),
            Error::RpcError(ref e) => e.description(),
            Error::XmlError(ref e) => e.description(),
            Error::ParseIntError(ref e) => e.description(),
            Error::ParseDateTimeError(ref e) => e.description(),
            Error::IoError(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&stderror::Error> {
        match *self {
            Error::TooManyRecords => None,
            Error::FnsError(_) => None,
            Error::ReqError(ref e) => e.cause(),
            Error::RpcError(ref e) => e.cause(),
            Error::XmlError(ref e) => e.cause(),
            Error::ParseIntError(ref e) => e.cause(),
            Error::ParseDateTimeError(ref e) => e.cause(),
            Error::IoError(ref e) => e.cause(),
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(other: reqwest::Error) -> Error {
        Error::ReqError(other)
    }
}

impl From<rpser::RpcError> for Error {
    fn from(other: rpser::RpcError) -> Error {
        Error::RpcError(other)
    }
}

impl From<rpser::xml::Error> for Error {
    fn from(other: rpser::xml::Error) -> Error {
        Error::XmlError(other)
    }
}

impl From<num::ParseIntError> for Error {
    fn from(other: num::ParseIntError) -> Error {
        Error::ParseIntError(other)
    }
}

impl From<chrono::ParseError> for Error {
    fn from(other: chrono::ParseError) -> Error {
        Error::ParseDateTimeError(other)
    }
}

impl From<io::Error> for Error {
    fn from(other: io::Error) -> Error {
        Error::IoError(other)
    }
}