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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::error;
use std::num::ParseIntError;
use curl::Error;

/// Error in [`Client`] calls
///
/// [`Client`]: transport/struct.YarClient.html
///
#[derive(Debug)]
pub enum YarError {
    /// Error when url not contains http or https
    URLError(&'static str),
    /// The underlying error is num::ParseIntError.
    TimeError(ParseIntError),
    CURLError(Error),
    /// remote call error
    CallErr(String),
}

impl From<ParseIntError> for YarError{
    fn from(err:ParseIntError) -> YarError {
        YarError::TimeError(err)
    }
}

impl From<Error> for YarError{
    fn from(err: Error) -> YarError {
        YarError::CURLError(err)
    }
}


impl error::Error for YarError{
    fn description(&self) -> &str {
        match *self {
            YarError::TimeError(ref err) => err.description(),
            YarError::CURLError(ref err) => err.description(),
            YarError::URLError(description)=> description,
            YarError::CallErr(ref description) => description.as_str(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            YarError::URLError(_) => None,
            YarError::TimeError(ref err)=> Some(err as &error::Error),
            YarError::CURLError(ref err)=> Some(err as &error::Error),
            YarError::CallErr(_)=> None,
        }
    }
}

use std::fmt;

impl fmt::Display for YarError{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            YarError::TimeError(ref err) => fmt::Display::fmt(err, f),
            YarError::CURLError(ref err) => fmt::Display::fmt(err, f),
            YarError::URLError(desc) => f.write_str(desc),
            YarError::CallErr(ref desc) => f.write_str(desc.as_str()),
        }
    }
}