imag/libimagstore/src/error.rs

205 lines
5.3 KiB
Rust
Raw Normal View History

2016-01-12 17:51:13 +00:00
use std::error::Error;
use std::fmt::Error as FmtError;
use std::clone::Clone;
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)]
pub enum StoreErrorKind {
FileError,
IdLocked,
IdNotFound,
OutOfMemory,
FileNotFound,
FileNotCreated,
2016-01-24 15:01:37 +00:00
IoError,
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,
HeaderPathSyntaxError,
HeaderPathTypeFailure,
HeaderKeyNotFound,
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,
// maybe more
}
fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str {
match e {
&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",
&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",
&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",
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",
}
}
impl Display for StoreErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "{}", store_error_type_as_str(self)));
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 {
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
*/
pub fn new(errtype: StoreErrorKind, cause: Option<Box<Error>>)
2016-01-16 12:39:53 +00:00
-> StoreError
{
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
*/
pub fn err_type(&self) -> StoreErrorKind {
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-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)),
}
}
}
#[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)
}
}