Skip to content

Commit

Permalink
Add impl std::error::Error for FluentError
Browse files Browse the repository at this point in the history
  • Loading branch information
guangie88 committed Feb 23, 2018
1 parent f696fba commit 35e585a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ byteorder = "^1.0.0"
clippy = {version = '0.0.116', optional = true}

[dev-dependencies]
failure = "~0.1.1"
tempdir = "~0.3"
41 changes: 41 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Implement error types for fruently crate.
use std::error;
use std::fmt;
use std::io;
use retry;
use serde_json;
Expand All @@ -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<io::Error> for FluentError {
fn from(err: io::Error) -> FluentError {
FluentError::IO(err)
Expand All @@ -39,3 +60,23 @@ impl From<serde_json::Error> for 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());
}
}

0 comments on commit 35e585a

Please sign in to comment.