diff options
author | Sascha Grunert <mail@saschagrunert.de> | 2016-11-16 15:00:57 +0100 |
---|---|---|
committer | Sascha Grunert <mail@saschagrunert.de> | 2016-11-16 15:00:57 +0100 |
commit | 762fa8c4c075c418a5884a517042ddeca24c616c (patch) | |
tree | d91128757a8ab344da0dca654a062f3177aebe18 | |
parent | c98f028d01af86abf2738984f6eda03006c9c649 (diff) |
Changed Error handling to Box<Error> approach
-rw-r--r-- | src/errors.rs | 57 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 6 |
3 files changed, 16 insertions, 49 deletions
diff --git a/src/errors.rs b/src/errors.rs index 64fd7e3..303658b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,27 +1,19 @@ //! Basic error handling mechanisms use std::error::Error; -use std::{fmt, io, num}; - -use git2; -use log; -use term; -use toml; +use std::fmt; /// The result type for GitJournal -pub type GitJournalResult<T> = Result<T, Box<GitJournalError>>; - -/// GitJournal error trait -pub trait GitJournalError: Error + Send + 'static {} +pub type GitJournalResult<T> = Result<T, Box<Error>>; /// Concrete errors -struct ConcreteGitJournalError { +struct GitJournalError { description: String, detail: Option<String>, cause: Option<Box<Error + Send>>, } -impl fmt::Display for ConcreteGitJournalError { +impl fmt::Display for GitJournalError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description)?; if let Some(ref s) = self.detail { @@ -31,16 +23,17 @@ impl fmt::Display for ConcreteGitJournalError { } } -impl fmt::Debug for ConcreteGitJournalError { +impl fmt::Debug for GitJournalError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self, f) } } -impl Error for ConcreteGitJournalError { +impl Error for GitJournalError { fn description(&self) -> &str { &self.description } + fn cause(&self) -> Option<&Error> { self.cause.as_ref().map(|c| { let e: &Error = &**c; @@ -49,43 +42,17 @@ impl Error for ConcreteGitJournalError { } } -/// Various error implementors -macro_rules! from_error { - ($($p:ty,)*) => ( - $(impl From<$p> for Box<GitJournalError> { - fn from(t: $p) -> Box<GitJournalError> { Box::new(t) } - })* - ) -} - -from_error! { - git2::Error, - io::Error, - log::ShutdownLoggerError, - num::ParseIntError, - term::Error, - toml::Error, -} - -impl GitJournalError for git2::Error {} -impl GitJournalError for io::Error {} -impl GitJournalError for log::ShutdownLoggerError {} -impl GitJournalError for num::ParseIntError {} -impl GitJournalError for term::Error {} -impl GitJournalError for toml::Error {} -impl GitJournalError for ConcreteGitJournalError {} - -/// Raise and internal error -pub fn internal_error(error: &str, detail: &str) -> Box<GitJournalError> { - Box::new(ConcreteGitJournalError { +/// Raise an internal error +pub fn internal_error(error: &str, detail: &str) -> Box<Error> { + Box::new(GitJournalError { description: error.to_string(), detail: Some(detail.to_string()), cause: None, }) } -pub fn internal(error: &fmt::Display) -> Box<GitJournalError> { - Box::new(ConcreteGitJournalError { +pub fn internal(error: &fmt::Display) -> Box<Error> { + Box::new(GitJournalError { description: error.to_string(), detail: None, cause: None, @@ -52,7 +52,7 @@ use rayon::prelude::*; use toml::Value; pub use config::Config; -pub use errors::{GitJournalResult, GitJournalError, internal_error}; +pub use errors::{GitJournalResult, internal_error}; use logger::Logger; use parser::{Parser, ParsedTag, Tags}; diff --git a/src/main.rs b/src/main.rs index ee8171c..af2a80f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,12 +7,12 @@ extern crate clap; extern crate log; use std::process::exit; -use std::{env, fs}; +use std::{env, fs, error}; use clap::{App, Shell}; -use gitjournal::{GitJournal, GitJournalResult, GitJournalError, internal_error}; +use gitjournal::{GitJournal, GitJournalResult, internal_error}; -fn error_and_exit(string: &str, error: Box<GitJournalError>) { +fn error_and_exit(string: &str, error: Box<error::Error>) { error!("{}: {}", string, error); exit(1); } |