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
//! Implement error types for fruently crate.

use std::error;
use std::fmt;
use std::io;
use retry;
use serde_json;
use rmp_serde::encode;

#[derive(Debug)]
pub enum FluentError {
    JsonEncode(serde_json::Error),
    MsgpackEncode(encode::Error),
    IO(io::Error),
    Retry(retry::RetryError),
    FileStored(String),
    #[doc(hidden)]
    Dummy(String),
}

impl fmt::Display for FluentError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            FluentError::JsonEncode(ref e) => write!(f, "Fluent JSON encode error: {}", e),
            FluentError::MsgpackEncode(ref e) => write!(f, "Fluent msgpack encode error: {}", e),
            FluentError::IO(ref e) => write!(f, "Fluent IO error: {}", e),
            FluentError::Retry(ref e) => write!(f, "Fluent retry error: {}", e),
            FluentError::FileStored(ref e) => write!(f, "Fluent file stored error: {}", e),
            FluentError::Dummy(ref e) => write!(f, "Fluent dummy error: {}", e),
        }
    }
}

impl error::Error for FluentError {
    fn description(&self) -> &str {
        "FluentError"
    }
}

impl From<io::Error> for FluentError {
    fn from(err: io::Error) -> FluentError {
        FluentError::IO(err)
    }
}

impl From<encode::Error> for FluentError {
    fn from(err: encode::Error) -> FluentError {
        FluentError::MsgpackEncode(err)
    }
}

impl From<retry::RetryError> for FluentError {
    fn from(err: retry::RetryError) -> FluentError {
        FluentError::Retry(err)
    }
}

impl From<serde_json::Error> for FluentError {
    fn from(err: serde_json::Error) -> FluentError {
        FluentError::JsonEncode(err)
    }
}

#[cfg(test)]
mod tests {
    extern crate failure;
    use super::*;
    use self::failure::Error;
    use std;

    type Result<T> = std::result::Result<T, Error>;

    #[test]
    fn test_failure_err() {
        let f = || -> Result<()> {
            Err(FluentError::Dummy("".to_owned()))?;
            Ok(())
        };

        assert!(f().is_err());
    }
}