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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use hyper;
use hyper::status::StatusCode;
use chrono;
use std::io::Error as IOError;
use xml::BuilderError as XMLError;
use std::io::Read;
use std::num;
// use xml;
use url::ParseError as URLParseError;
use azure::core::enumerations::ParsingError;
use azure::core::range::ParseError;

#[derive(Debug, Clone, PartialEq)]
pub struct UnexpectedHTTPResult {
    expected: StatusCode,
    received: StatusCode,
    body: String,
}

impl UnexpectedHTTPResult {
    pub fn new(expected: StatusCode, received: StatusCode, body: &str) -> UnexpectedHTTPResult {
        UnexpectedHTTPResult {
            expected: expected,
            received: received,
            body: body.to_owned(),
        }
    }
}

#[derive(Debug)]
pub enum AzureError {
    HyperError(hyper::error::Error),
    IOError(IOError),
    XMLError(XMLError),
    UnexpectedHTTPResult(UnexpectedHTTPResult),
    HeaderNotFound(String),
    ResponseParsingError(TraversingError),
    ParseIntError(num::ParseIntError),
    ParseError(ParseError),
    GenericError,
    ParsingError(ParsingError),
    InputParametersError(String),
    URLParseError(URLParseError),
}

#[derive(Debug)]
pub enum TraversingError {
    PathNotFound(String),
    MultipleNode(String),
    EnumerationNotMatched(String),
    DateTimeParseError(chrono::format::ParseError),
    TextNotFound,
    ParseIntError(num::ParseIntError),
    GenericParseError(String),
    ParsingError(ParsingError),
}

impl From<URLParseError> for AzureError {
    fn from(upe: URLParseError) -> AzureError {
        AzureError::URLParseError(upe)
    }
}

impl From<ParseError> for AzureError {
    fn from(pe: ParseError) -> AzureError {
        AzureError::ParseError(pe)
    }
}

impl From<()> for AzureError {
    fn from(_: ()) -> AzureError {
        AzureError::GenericError
    }
}

impl From<hyper::error::Error> for AzureError {
    fn from(he: hyper::error::Error) -> AzureError {
        AzureError::HyperError(he)
    }
}

impl From<ParsingError> for AzureError {
    fn from(pie: ParsingError) -> AzureError {
        AzureError::ParsingError(pie)
    }
}

impl From<XMLError> for AzureError {
    fn from(xmle: XMLError) -> AzureError {
        AzureError::XMLError(xmle)
    }
}

impl From<IOError> for AzureError {
    fn from(ioe: IOError) -> AzureError {
        AzureError::IOError(ioe)
    }
}

impl From<chrono::format::ParseError> for AzureError {
    fn from(pe: chrono::format::ParseError) -> AzureError {
        AzureError::ResponseParsingError(TraversingError::DateTimeParseError(pe))
    }
}

impl From<TraversingError> for AzureError {
    fn from(te: TraversingError) -> AzureError {
        AzureError::ResponseParsingError(te)
    }
}

impl From<num::ParseIntError> for AzureError {
    fn from(pie: num::ParseIntError) -> AzureError {
        AzureError::ParseIntError(pie)
    }
}

impl From<chrono::format::ParseError> for TraversingError {
    fn from(pe: chrono::format::ParseError) -> TraversingError {
        TraversingError::DateTimeParseError(pe)
    }
}

impl From<num::ParseIntError> for TraversingError {
    fn from(pie: num::ParseIntError) -> TraversingError {
        TraversingError::ParseIntError(pie)
    }
}

impl From<ParsingError> for TraversingError {
    fn from(pie: ParsingError) -> TraversingError {
        TraversingError::ParsingError(pie)
    }
}

#[inline]
pub fn check_status(resp: &mut hyper::client::response::Response,
                    s: StatusCode)
                    -> Result<(), AzureError> {
    if resp.status != s {
        let mut resp_s = String::new();
        try!(resp.read_to_string(&mut resp_s));

        return Err(AzureError::UnexpectedHTTPResult(UnexpectedHTTPResult::new(s,
                                                                              resp.status,
                                                                              &resp_s)));
    }

    Ok(())
}