-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from pingcap/siddontang/update-error
update error
- Loading branch information
Showing
2 changed files
with
39 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters