Replace error code with macro for code generation

This commit is contained in:
Matthias Beyer 2016-05-15 16:40:06 +02:00
parent a69fd78058
commit 627fa7de46

View file

@ -1,138 +1,45 @@
use std::error::Error; use std::error::Error;
use std::fmt::Error as FmtError; use std::fmt::Error as FmtError;
use std::fmt::{Debug, Display, Formatter}; use std::fmt::{Display, Formatter};
use std::fmt;
use std::convert::From; use std::convert::From;
/** generate_error_types!(StoreError, StoreErrorKind,
* Kind of store error ConfigurationError => "Store Configuration Error",
*/ FileError => "File Error",
#[derive(Clone, Copy, Debug, PartialEq)] IoError => "IO Error",
pub enum StoreErrorKind { IdLocked => "ID locked",
ConfigurationError, IdNotFound => "ID not found",
FileError, OutOfMemory => "Out of Memory",
IdLocked, FileNotFound => "File corresponding to ID not found",
IdNotFound, FileNotCreated => "File corresponding to ID could not be created",
OutOfMemory, StorePathExists => "Store path exists",
FileNotFound, StorePathCreate => "Store path create",
FileNotCreated, LockError => "Error locking datastructure",
IoError, LockPoisoned => "The internal Store Lock has been poisoned",
StorePathExists, EntryAlreadyBorrowed => "Entry is already borrowed",
StorePathCreate, EntryAlreadyExists => "Entry already exists",
LockError, MalformedEntry => "Entry has invalid formatting, missing header",
LockPoisoned, HeaderPathSyntaxError => "Syntax error in accessor string",
EntryAlreadyBorrowed, HeaderPathTypeFailure => "Header has wrong type for path",
EntryAlreadyExists, HeaderKeyNotFound => "Header Key not found",
MalformedEntry, HeaderTypeFailure => "Header type is wrong",
HeaderPathSyntaxError, HookRegisterError => "Hook register error",
HeaderPathTypeFailure, AspectNameNotFoundError => "Aspect name not found",
HeaderKeyNotFound, HookExecutionError => "Hook execution error",
HeaderTypeFailure, PreHookExecuteError => "Pre-Hook execution error",
HookRegisterError, PostHookExecuteError => "Post-Hook execution error",
AspectNameNotFoundError, StorePathLacksVersion => "The supplied store path has no version part",
HookExecutionError, GlobError => "glob() error",
PreHookExecuteError, EncodingError => "Encoding error"
PostHookExecuteError, );
StorePathLacksVersion,
GlobError,
EncodingError,
// maybe more
}
fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { generate_error_types!(ParserError, ParserErrorKind,
match *e { TOMLParserErrors => "Several TOML-Parser-Errors",
StoreErrorKind::ConfigurationError => "Store Configuration Error", MissingMainSection => "Missing main section",
StoreErrorKind::FileError | MissingVersionInfo => "Missing version information in main section",
StoreErrorKind::IoError => "File Error", NonTableInBaseTable => "A non-table was found in the base table",
StoreErrorKind::IdLocked => "ID locked", HeaderInconsistency => "The header is inconsistent"
StoreErrorKind::IdNotFound => "ID not found", );
StoreErrorKind::OutOfMemory => "Out of Memory",
StoreErrorKind::FileNotFound => "File corresponding to ID not found",
StoreErrorKind::FileNotCreated => "File corresponding to ID could not be created",
StoreErrorKind::StorePathExists |
StoreErrorKind::StorePathCreate => "Store path create",
StoreErrorKind::LockError => "Error locking datastructure",
StoreErrorKind::LockPoisoned
=> "The internal Store Lock has been poisoned",
StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed",
StoreErrorKind::EntryAlreadyExists => "Entry already exists",
StoreErrorKind::MalformedEntry => "Entry has invalid formatting, missing header",
StoreErrorKind::HeaderPathSyntaxError => "Syntax error in accessor string",
StoreErrorKind::HeaderPathTypeFailure => "Header has wrong type for path",
StoreErrorKind::HeaderKeyNotFound => "Header Key not found",
StoreErrorKind::HeaderTypeFailure => "Header type is wrong",
StoreErrorKind::HookRegisterError => "Hook register error",
StoreErrorKind::AspectNameNotFoundError => "Aspect name not found",
StoreErrorKind::HookExecutionError => "Hook execution error",
StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error",
StoreErrorKind::PostHookExecuteError => "Post-Hook execution error",
StoreErrorKind::StorePathLacksVersion => "The supplied store path has no version part",
StoreErrorKind::GlobError => "glob() error",
StoreErrorKind::EncodingError => "Encoding error",
}
}
impl Display for StoreErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "{}", store_error_type_as_str(self)));
Ok(())
}
}
/**
* Store error type
*/
#[derive(Debug)]
pub struct StoreError {
err_type: StoreErrorKind,
cause: Option<Box<Error>>,
}
impl StoreError {
/**
* Build a new StoreError from an StoreErrorKind, optionally with cause
*/
pub fn new(errtype: StoreErrorKind, cause: Option<Box<Error>>)
-> StoreError
{
StoreError {
err_type: errtype,
cause: cause,
}
}
/**
* Get the error type of this StoreError
*/
pub fn err_type(&self) -> StoreErrorKind {
self.err_type
}
}
impl Display for StoreError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "[{}]", store_error_type_as_str(&self.err_type)));
Ok(())
}
}
impl Error for StoreError {
fn description(&self) -> &str {
store_error_type_as_str(&self.err_type)
}
fn cause(&self) -> Option<&Error> {
self.cause.as_ref().map(|e| &**e)
}
}
impl From<ParserError> for StoreError { impl From<ParserError> for StoreError {
fn from(ps: ParserError) -> StoreError { fn from(ps: ParserError) -> StoreError {
@ -152,64 +59,3 @@ impl From<::std::io::Error> for StoreError {
} }
} }
#[derive(Clone)]
pub enum ParserErrorKind {
TOMLParserErrors,
MissingMainSection,
MissingVersionInfo,
NonTableInBaseTable,
HeaderInconsistency,
}
pub struct ParserError {
kind: ParserErrorKind,
cause: Option<Box<Error>>,
}
impl ParserError {
pub fn new(k: ParserErrorKind, cause: Option<Box<Error>>) -> ParserError {
ParserError {
kind: k,
cause: cause,
}
}
}
impl Debug for ParserError {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
try!(write!(f, "{:?}", self.description()));
Ok(())
}
}
impl Display for ParserError {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
try!(write!(f, "{}", self.description()));
Ok(())
}
}
impl Error for ParserError {
fn description(&self) -> &str {
match self.kind {
ParserErrorKind::TOMLParserErrors => "Several TOML-Parser-Errors",
ParserErrorKind::MissingMainSection => "Missing main section",
ParserErrorKind::MissingVersionInfo => "Missing version information in main section",
ParserErrorKind::NonTableInBaseTable => "A non-table was found in the base table",
ParserErrorKind::HeaderInconsistency => "The header is inconsistent",
}
}
fn cause(&self) -> Option<&Error> {
self.cause.as_ref().map(|e| &**e)
}
}