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
use std::error::Error;
use std::{fmt, io};

/// This type represents the possible errors that can occur while
/// sending DogstatsD metrics.
#[derive(Debug)]
pub enum DogstatsdError {
    /// Chained IO errors.
    IoError(io::Error),
}

use self::DogstatsdError::*;

impl fmt::Display for DogstatsdError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            IoError(ref error) => write!(f, "{}", error),
        }
    }
}

impl Error for DogstatsdError {
    fn description(&self) -> &str {
        match *self {
            IoError(ref error) => error.description(),
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::DogstatsdError;
    use std::io;

    #[test]
    fn test_error_display() {
        let err = DogstatsdError::from(io::Error::new(io::ErrorKind::Other, "oh no!"));
        assert_eq!(format!("{}", err), "oh no!".to_owned());
    }
}