Rewrite error module
This commit is contained in:
parent
053c5c13e8
commit
f55f1e627e
1 changed files with 10 additions and 64 deletions
|
@ -1,66 +1,12 @@
|
|||
use std::error::Error;
|
||||
use std::clone::Clone;
|
||||
use std::fmt::Error as FmtError;
|
||||
use std::fmt::{Display, Formatter};
|
||||
generate_error_module!(
|
||||
generate_error_types!(TodoError, TodoErrorKind,
|
||||
ConversionError => "Conversion Error",
|
||||
StoreError => "Store Error",
|
||||
ImportError => "Error importing"
|
||||
);
|
||||
);
|
||||
|
||||
/// Enum of Error Types, as of now we have two:
|
||||
/// * ConversionError: for Errors concerning conversion failures from task_hookrs::task::Task to
|
||||
/// libimagtodo::task::Task. unused.
|
||||
/// * StoreError: For Errors thrown by functions of the Store/structs relates to the Store
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum TodoErrorKind {
|
||||
ConversionError,
|
||||
StoreError,
|
||||
}
|
||||
pub use self::error::TodoError;
|
||||
pub use self::error::TodoErrorKind;
|
||||
pub use self::error::MapErrInto;
|
||||
|
||||
/// Maps a TodoErrorKind to a String
|
||||
fn todo_error_type_as_str(e: &TodoErrorKind) -> &'static str {
|
||||
match e {
|
||||
&TodoErrorKind::ConversionError => "Conversion Error",
|
||||
&TodoErrorKind::StoreError => "Store Error",
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TodoErrorKind {
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "{}", todo_error_type_as_str(self)));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Error struct for the imag-todo module
|
||||
#[derive(Debug)]
|
||||
pub struct TodoError {
|
||||
err_type : TodoErrorKind,
|
||||
cause : Option<Box<Error>>,
|
||||
}
|
||||
|
||||
impl TodoError {
|
||||
/// Creates a new TodoError, with TodoErrorKind errtype and an optional cause
|
||||
pub fn new(errtype : TodoErrorKind, cause : Option<Box<Error>>) -> TodoError {
|
||||
TodoError {
|
||||
err_type : errtype,
|
||||
cause : cause,
|
||||
}
|
||||
}
|
||||
/// Returns the error type (TodoErrorKind)
|
||||
pub fn err_type(&self) -> TodoErrorKind {
|
||||
self.err_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TodoError {
|
||||
fn fmt(&self, fmt : &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "[{}]", todo_error_type_as_str(&self.err_type.clone())));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for TodoError {
|
||||
fn description(&self) -> &str {
|
||||
todo_error_type_as_str(&self.err_type.clone())
|
||||
}
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
self.cause.as_ref().map(|e| &**e)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue