diff --git a/Cargo.toml b/Cargo.toml index 4ec65e1..b04aad3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,5 @@ byteorder = "^1.0.0" clippy = {version = '0.0.116', optional = true} [dev-dependencies] +failure = "~0.1.1" tempdir = "~0.3" diff --git a/src/error.rs b/src/error.rs index 143236a..8972202 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,7 @@ //! Implement error types for fruently crate. +use std::error; +use std::fmt; use std::io; use retry; use serde_json; @@ -16,6 +18,25 @@ pub enum FluentError { 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 for FluentError { fn from(err: io::Error) -> FluentError { FluentError::IO(err) @@ -39,3 +60,23 @@ impl From for FluentError { FluentError::JsonEncode(err) } } + +#[cfg(test)] +mod tests { + extern crate failure; + use super::*; + use self::failure::Error; + use std; + + type Result = std::result::Result; + + #[test] + fn test_failure_err() { + let f = || -> Result<()> { + Err(FluentError::Dummy("".to_owned()))?; + Ok(()) + }; + + assert!(f().is_err()); + } +}