Skip to content

Commit

Permalink
Merge pull request #11 from pingcap/siddontang/update-error
Browse files Browse the repository at this point in the history
update error
  • Loading branch information
ngaut committed Jan 8, 2016
2 parents a15f5a6 + dc5ca80 commit 87c0457
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
38 changes: 37 additions & 1 deletion src/raft/errors.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
use std::{result, io, fmt};
use std::error;

use protobuf::ProtobufError;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum Error {
Io(io::Error),
NotFound,
Protobuf(ProtobufError),
Other(String),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NotFound => write!(f, "not found"),
Error::Io(ref error) => fmt::Display::fmt(error, f),
_ => fmt::Display::fmt(self, f),
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
match self {
// not sure that cause should be included in message
&Error::Io(ref e) => e.description(),
&Error::NotFound => "not found",
&Error::Protobuf(ref e) => e.description(),
&Error::Other(ref e) => &e,
}
}

fn cause(&self) -> Option<&error::Error> {
match self {
&Error::Io(ref e) => Some(e),
&Error::Protobuf(ref e) => Some(e),
_ => None,
}
}
}

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

impl From<ProtobufError> for Error {
fn from(err: ProtobufError) -> Error {
Error::Protobuf(err)
}
}

pub type Result<T> = result::Result<T, Error>;
4 changes: 2 additions & 2 deletions src/raft/raft_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use raft::storage::Storage;


/// Raft log implementation
pub struct RaftLog {
store: Storage,
pub struct RaftLog<T: Storage> {
store: T,
}

0 comments on commit 87c0457

Please sign in to comment.