diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-05-15 16:40:06 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-05-17 00:26:35 +0200 |
commit | 627fa7de46701a1848ffe8c464cc7dc4a1618b0c (patch) | |
tree | 6f279b87a8090848fe502adb9ec21847bc413fb2 /libimagstore | |
parent | a69fd780589db82aad7756d579c523aabc08fd13 (diff) |
Replace error code with macro for code generation
Diffstat (limited to 'libimagstore')
-rw-r--r-- | libimagstore/src/error.rs | 230 |
1 files changed, 38 insertions, 192 deletions
diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index 1976622d..ad36963e 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -1,138 +1,45 @@ use std::error::Error; use std::fmt::Error as FmtError; -use std::fmt::{Debug, Display, Formatter}; -use std::fmt; +use std::fmt::{Display, Formatter}; use std::convert::From; -/** - * Kind of store error - */ -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum StoreErrorKind { - ConfigurationError, - FileError, - IdLocked, - IdNotFound, - OutOfMemory, - FileNotFound, - FileNotCreated, - IoError, - StorePathExists, - StorePathCreate, - LockError, - LockPoisoned, - EntryAlreadyBorrowed, - EntryAlreadyExists, - MalformedEntry, - HeaderPathSyntaxError, - HeaderPathTypeFailure, - HeaderKeyNotFound, - HeaderTypeFailure, - HookRegisterError, - AspectNameNotFoundError, - HookExecutionError, - PreHookExecuteError, - PostHookExecuteError, - StorePathLacksVersion, - GlobError, - EncodingError, - // maybe more -} - -fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { - match *e { - StoreErrorKind::ConfigurationError => "Store Configuration Error", - StoreErrorKind::FileError | - StoreErrorKind::IoError => "File Error", - StoreErrorKind::IdLocked => "ID locked", - 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) - } - -} +generate_error_types!(StoreError, StoreErrorKind, + ConfigurationError => "Store Configuration Error", + FileError => "File Error", + IoError => "IO Error", + IdLocked => "ID locked", + IdNotFound => "ID not found", + OutOfMemory => "Out of Memory", + FileNotFound => "File corresponding to ID not found", + FileNotCreated => "File corresponding to ID could not be created", + StorePathExists => "Store path exists", + StorePathCreate => "Store path create", + LockError => "Error locking datastructure", + LockPoisoned => "The internal Store Lock has been poisoned", + EntryAlreadyBorrowed => "Entry is already borrowed", + EntryAlreadyExists => "Entry already exists", + MalformedEntry => "Entry has invalid formatting, missing header", + HeaderPathSyntaxError => "Syntax error in accessor string", + HeaderPathTypeFailure => "Header has wrong type for path", + HeaderKeyNotFound => "Header Key not found", + HeaderTypeFailure => "Header type is wrong", + HookRegisterError => "Hook register error", + AspectNameNotFoundError => "Aspect name not found", + HookExecutionError => "Hook execution error", + PreHookExecuteError => "Pre-Hook execution error", + PostHookExecuteError => "Post-Hook execution error", + StorePathLacksVersion => "The supplied store path has no version part", + GlobError => "glob() error", + EncodingError => "Encoding error" +); + +generate_error_types!(ParserError, ParserErrorKind, + TOMLParserErrors => "Several TOML-Parser-Errors", + MissingMainSection => "Missing main section", + MissingVersionInfo => "Missing version information in main section", + NonTableInBaseTable => "A non-table was found in the base table", + HeaderInconsistency => "The header is inconsistent" +); impl From<ParserError> for 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) - } - -} - |