Introduce error module

This commit is contained in:
Matthias Beyer 2016-03-11 16:16:04 +01:00
parent c1fcf22619
commit 34c020fa55
2 changed files with 94 additions and 0 deletions

89
imag-view/src/error.rs Normal file
View file

@ -0,0 +1,89 @@
use std::error::Error;
use std::fmt::Error as FmtError;
use std::clone::Clone;
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
use std::convert::From;
/**
* Kind of store error
*/
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ViewErrorKind {
StoreError,
NoVersion,
PatternError,
GlobBuildError,
}
fn view_error_type_as_str(e: &ViewErrorKind) -> &'static str {
match e {
&ViewErrorKind::StoreError => "Store error",
&ViewErrorKind::NoVersion => "No version specified",
&ViewErrorKind::PatternError => "Error in Pattern",
&ViewErrorKind::GlobBuildError => "Could not build glob() Argument",
}
}
impl Display for ViewErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "{}", view_error_type_as_str(self)));
Ok(())
}
}
/**
* View error type
*/
#[derive(Debug)]
pub struct ViewError {
err_type: ViewErrorKind,
cause: Option<Box<Error>>,
}
impl ViewError {
/**
* Build a new ViewError from an ViewErrorKind, optionally with cause
*/
pub fn new(errtype: ViewErrorKind, cause: Option<Box<Error>>)
-> ViewError
{
ViewError {
err_type: errtype,
cause: cause,
}
}
/**
* Get the error type of this ViewError
*/
pub fn err_type(&self) -> ViewErrorKind {
self.err_type.clone()
}
}
impl Display for ViewError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "[{}]", view_error_type_as_str(&self.err_type.clone())));
Ok(())
}
}
impl Error for ViewError {
fn description(&self) -> &str {
view_error_type_as_str(&self.err_type.clone())
}
fn cause(&self) -> Option<&Error> {
self.cause.as_ref().map(|e| &**e)
}
}

View file

@ -9,6 +9,7 @@ extern crate libimagrt;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
use std::result::Result as RResult;
use std::process::exit; use std::process::exit;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
@ -16,14 +17,18 @@ use libimagstore::store::FileLockEntry;
use libimagstore::store::Result as StoreResult; use libimagstore::store::Result as StoreResult;
use libimagutil::trace::trace_error; use libimagutil::trace::trace_error;
mod error;
mod ui; mod ui;
mod viewer; mod viewer;
use error::{ViewError, ViewErrorKind};
use ui::build_ui; use ui::build_ui;
use viewer::Viewer; use viewer::Viewer;
use viewer::ViewInformation; use viewer::ViewInformation;
use viewer::stdout::StdoutViewer; use viewer::stdout::StdoutViewer;
type Result<T> = RResult<T, ViewError>;
fn main() { fn main() {
let name = "imag-view"; let name = "imag-view";
let version = &version!()[..]; let version = &version!()[..];