Skip to content

Commit

Permalink
Merge pull request #3 from guangie88/error-impl
Browse files Browse the repository at this point in the history
Add impl std::error::Error for FluentError
  • Loading branch information
cosmo0920 authored Feb 25, 2018
2 parents f696fba + 35e585a commit 46e1da6
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 46e1da6

Please sign in to comment.