Skip to main content

dogstatsd/
error.rs

1use std::error::Error;
2use std::{fmt, io};
3
4/// This type represents the possible errors that can occur while
5/// sending DogstatsD metrics.
6#[derive(Debug)]
7pub enum DogstatsdError {
8    /// Chained IO errors.
9    IoError(io::Error),
10}
11
12use self::DogstatsdError::*;
13
14impl fmt::Display for DogstatsdError {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match *self {
17            IoError(ref error) => write!(f, "{}", error),
18        }
19    }
20}
21
22impl Error for DogstatsdError {
23    fn source(&self) -> Option<&(dyn Error + 'static)> {
24        match self {
25            IoError(error) => Some(error),
26        }
27    }
28}
29
30impl From<io::Error> for DogstatsdError {
31    fn from(e: io::Error) -> Self {
32        IoError(e)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::DogstatsdError;
39    use std::io;
40
41    #[test]
42    fn test_error_display() {
43        let err = DogstatsdError::from(io::Error::other("oh no!"));
44        assert_eq!(format!("{}", err), "oh no!".to_owned());
45    }
46}