2016-01-12 17:51:13 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::Error as FmtError;
|
|
|
|
use std::clone::Clone;
|
2016-01-23 15:26:02 +00:00
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
|
|
|
use std::fmt;
|
2016-01-23 16:30:01 +00:00
|
|
|
use std::convert::From;
|
2016-01-12 17:51:13 +00:00
|
|
|
|
2016-01-21 20:31:48 +00:00
|
|
|
/**
|
|
|
|
* Kind of store error
|
|
|
|
*/
|
2016-01-23 16:03:21 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2016-01-16 13:09:34 +00:00
|
|
|
pub enum StoreErrorKind {
|
2016-03-05 10:36:29 +00:00
|
|
|
ConfigurationError,
|
2016-01-17 17:01:52 +00:00
|
|
|
FileError,
|
2016-01-17 15:20:58 +00:00
|
|
|
IdLocked,
|
2016-01-16 12:36:19 +00:00
|
|
|
IdNotFound,
|
|
|
|
OutOfMemory,
|
2016-01-17 17:27:19 +00:00
|
|
|
FileNotFound,
|
|
|
|
FileNotCreated,
|
2016-01-24 15:01:37 +00:00
|
|
|
IoError,
|
2016-01-17 22:12:38 +00:00
|
|
|
StorePathExists,
|
2016-01-18 22:00:39 +00:00
|
|
|
StorePathCreate,
|
2016-01-17 15:58:58 +00:00
|
|
|
LockPoisoned,
|
2016-01-23 16:30:01 +00:00
|
|
|
EntryAlreadyBorrowed,
|
2016-01-24 16:28:52 +00:00
|
|
|
EntryAlreadyExists,
|
2016-01-23 16:30:01 +00:00
|
|
|
MalformedEntry,
|
2016-01-27 09:37:43 +00:00
|
|
|
HeaderPathSyntaxError,
|
2016-02-04 11:23:28 +00:00
|
|
|
HeaderPathTypeFailure,
|
|
|
|
HeaderKeyNotFound,
|
2016-02-11 14:21:26 +00:00
|
|
|
HeaderTypeFailure,
|
2016-02-15 21:14:13 +00:00
|
|
|
HookRegisterError,
|
2016-02-23 18:34:28 +00:00
|
|
|
HookExecutionError,
|
2016-02-16 19:42:36 +00:00
|
|
|
PreHookExecuteError,
|
|
|
|
PostHookExecuteError,
|
2016-03-13 19:56:23 +00:00
|
|
|
StorePathLacksVersion,
|
2016-03-13 13:32:48 +00:00
|
|
|
GlobError,
|
2016-03-18 12:46:16 +00:00
|
|
|
EncodingError,
|
2016-01-23 15:26:02 +00:00
|
|
|
// maybe more
|
2016-01-16 12:36:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 13:09:34 +00:00
|
|
|
fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
|
2016-01-16 13:06:55 +00:00
|
|
|
match e {
|
2016-03-05 10:36:29 +00:00
|
|
|
&StoreErrorKind::ConfigurationError => "Store Configuration Error",
|
2016-01-17 22:12:38 +00:00
|
|
|
&StoreErrorKind::FileError => "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",
|
2016-01-24 15:01:37 +00:00
|
|
|
&StoreErrorKind::IoError => "File Error",
|
2016-01-17 22:12:38 +00:00
|
|
|
&StoreErrorKind::StorePathExists => "Store path exists",
|
2016-01-18 22:00:39 +00:00
|
|
|
&StoreErrorKind::StorePathCreate => "Store path create",
|
2016-01-17 15:58:58 +00:00
|
|
|
&StoreErrorKind::LockPoisoned
|
|
|
|
=> "The internal Store Lock has been poisoned",
|
2016-01-23 16:30:01 +00:00
|
|
|
&StoreErrorKind::EntryAlreadyBorrowed => "Entry is already borrowed",
|
2016-01-24 16:28:52 +00:00
|
|
|
&StoreErrorKind::EntryAlreadyExists => "Entry already exists",
|
2016-01-23 16:30:01 +00:00
|
|
|
&StoreErrorKind::MalformedEntry => "Entry has invalid formatting, missing header",
|
2016-01-27 09:37:43 +00:00
|
|
|
&StoreErrorKind::HeaderPathSyntaxError => "Syntax error in accessor string",
|
2016-02-04 11:23:28 +00:00
|
|
|
&StoreErrorKind::HeaderPathTypeFailure => "Header has wrong type for path",
|
|
|
|
&StoreErrorKind::HeaderKeyNotFound => "Header Key not found",
|
2016-02-11 14:21:26 +00:00
|
|
|
&StoreErrorKind::HeaderTypeFailure => "Header type is wrong",
|
2016-02-15 21:14:13 +00:00
|
|
|
&StoreErrorKind::HookRegisterError => "Hook register error",
|
2016-02-23 19:05:02 +00:00
|
|
|
&StoreErrorKind::HookExecutionError => "Hook execution error",
|
2016-02-16 19:42:36 +00:00
|
|
|
&StoreErrorKind::PreHookExecuteError => "Pre-Hook execution error",
|
|
|
|
&StoreErrorKind::PostHookExecuteError => "Post-Hook execution error",
|
2016-03-13 19:56:23 +00:00
|
|
|
&StoreErrorKind::StorePathLacksVersion => "The supplied store path has no version part",
|
2016-03-13 13:32:48 +00:00
|
|
|
&StoreErrorKind::GlobError => "glob() error",
|
2016-03-18 12:46:16 +00:00
|
|
|
&StoreErrorKind::EncodingError => "Encoding error",
|
2016-01-16 12:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 13:09:34 +00:00
|
|
|
impl Display for StoreErrorKind {
|
2016-01-16 12:36:19 +00:00
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
2016-01-16 13:06:55 +00:00
|
|
|
try!(write!(fmt, "{}", store_error_type_as_str(self)));
|
2016-01-16 12:36:19 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:31:48 +00:00
|
|
|
/**
|
|
|
|
* Store error type
|
|
|
|
*/
|
2016-01-16 13:29:09 +00:00
|
|
|
#[derive(Debug)]
|
2016-01-12 17:51:13 +00:00
|
|
|
pub struct StoreError {
|
2016-01-16 13:09:34 +00:00
|
|
|
err_type: StoreErrorKind,
|
2016-01-12 17:51:13 +00:00
|
|
|
cause: Option<Box<Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StoreError {
|
|
|
|
|
2016-01-21 20:31:48 +00:00
|
|
|
/**
|
|
|
|
* Build a new StoreError from an StoreErrorKind, optionally with cause
|
|
|
|
*/
|
2016-01-16 13:09:34 +00:00
|
|
|
pub fn new(errtype: StoreErrorKind, cause: Option<Box<Error>>)
|
2016-01-16 12:39:53 +00:00
|
|
|
-> StoreError
|
2016-01-23 15:26:02 +00:00
|
|
|
{
|
|
|
|
StoreError {
|
|
|
|
err_type: errtype,
|
|
|
|
cause: cause,
|
|
|
|
}
|
2016-01-12 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 20:31:48 +00:00
|
|
|
/**
|
|
|
|
* Get the error type of this StoreError
|
|
|
|
*/
|
2016-01-16 13:09:34 +00:00
|
|
|
pub fn err_type(&self) -> StoreErrorKind {
|
2016-01-16 12:36:19 +00:00
|
|
|
self.err_type.clone()
|
|
|
|
}
|
|
|
|
|
2016-01-12 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for StoreError {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
2016-01-16 13:08:39 +00:00
|
|
|
try!(write!(fmt, "[{}]", store_error_type_as_str(&self.err_type.clone())));
|
2016-01-12 17:51:13 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for StoreError {
|
|
|
|
|
|
|
|
fn description(&self) -> &str {
|
2016-01-16 13:08:39 +00:00
|
|
|
store_error_type_as_str(&self.err_type.clone())
|
2016-01-12 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
self.cause.as_ref().map(|e| &**e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-23 16:30:01 +00:00
|
|
|
impl From<ParserError> for StoreError {
|
|
|
|
fn from(ps: ParserError) -> StoreError {
|
|
|
|
StoreError {
|
|
|
|
err_type: StoreErrorKind::MalformedEntry,
|
|
|
|
cause: Some(Box::new(ps)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-23 15:26:02 +00:00
|
|
|
|
2016-01-24 15:01:37 +00:00
|
|
|
impl From<::std::io::Error> for StoreError {
|
|
|
|
fn from(ps: ::std::io::Error) -> StoreError {
|
|
|
|
StoreError {
|
|
|
|
err_type: StoreErrorKind::IoError,
|
|
|
|
cause: Some(Box::new(ps)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum ParserErrorKind {
|
|
|
|
TOMLParserErrors,
|
|
|
|
MissingMainSection,
|
|
|
|
MissingVersionInfo,
|
2016-02-06 18:26:00 +00:00
|
|
|
NonTableInBaseTable,
|
|
|
|
HeaderInconsistency,
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2016-02-06 18:26:00 +00:00
|
|
|
ParserErrorKind::NonTableInBaseTable => "A non-table was found in the base table",
|
|
|
|
ParserErrorKind::HeaderInconsistency => "The header is inconsistent",
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
self.cause.as_ref().map(|e| &**e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|