summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalte Brandy <malte.brandy@maralorn.de>2018-04-23 20:21:50 +0200
committerGitHub <noreply@github.com>2018-04-23 20:21:50 +0200
commit448a0a6dce877b64d7c5ae9db89e2d14450b84c1 (patch)
tree3df0d2d5d2123be13bc0d497ff59450d8b282626
parent146ed281c586af4dc1337b2d1f72546130b4c127 (diff)
parentffae47bef5d3c87268c57fe329ce954f757d37b3 (diff)
Merge pull request #67 from maralorn/error-chain
Refactor error handling to use error_chain
-rw-r--r--Cargo.toml1
-rw-r--r--src/error.rs84
-rw-r--r--src/import.rs17
-rw-r--r--src/lib.rs6
-rw-r--r--src/result.rs14
5 files changed, 16 insertions, 106 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 31381f2..d5ee414 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,6 +26,7 @@ serde_json = "1"
uuid = { version = "0.6", features = ["serde", "v4"] }
log = "0.4"
derive_builder = "0.5.1"
+error-chain = "0.11.0"
[dev-dependencies]
env_logger = "0.4"
diff --git a/src/error.rs b/src/error.rs
index 0229d6e..c7bf0e6 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,83 +5,11 @@
//
//! Error module, containing error types
-
-use std::error::Error;
-use std::fmt::Error as FmtError;
-use std::clone::Clone;
-use std::fmt::{Display, Formatter};
-
-///
-/// Kind of task error
-///
-#[derive(Clone, Copy, Debug, PartialEq)]
-pub enum TaskErrorKind {
- /// Error kind indicating that the JSON parser failed
- ParserError,
-
- /// Error kind indicating that the Status of a task is missing
- NoStatus,
-
- /// Error kind indicating that the Reader failed to read something
- ReaderError,
-}
-
-fn store_error_type_as_str(e: &TaskErrorKind) -> &'static str {
- match e {
- &TaskErrorKind::ParserError => "Parser Error",
- &TaskErrorKind::NoStatus => "Task status is missing",
- &TaskErrorKind::ReaderError => "Reader Error",
- }
-}
-
-impl Display for TaskErrorKind {
- fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
- write!(fmt, "{}", store_error_type_as_str(self))?;
- Ok(())
- }
-}
-
-///
-/// Task error type
-///
-#[derive(Debug)]
-pub struct TaskError {
- err_type: TaskErrorKind,
- cause: Option<Box<Error>>,
-}
-
-impl TaskError {
- ///
- /// Build a new TaskError from an TaskErrorKind, optionally with cause
- ///
- pub fn new(errtype: TaskErrorKind, cause: Option<Box<Error>>) -> TaskError {
- TaskError {
- err_type: errtype,
- cause: cause,
- }
- }
-
- ///
- /// Get the error type of this TaskError
- ///
- pub fn err_type(&self) -> TaskErrorKind {
- self.err_type.clone()
- }
-}
-
-impl Display for TaskError {
- fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
- write!(fmt, "[{}]", store_error_type_as_str(&self.err_type.clone()))?;
- Ok(())
- }
-}
-
-impl Error for TaskError {
- fn description(&self) -> &str {
- store_error_type_as_str(&self.err_type.clone())
- }
-
- fn cause(&self) -> Option<&Error> {
- self.cause.as_ref().map(|e| &**e)
+error_chain!{
+ errors {
+ /// Error kind indicating that the JSON parser failed
+ ParserError {}
+ /// Error kind indicating that the Reader failed to read something
+ ReaderError {}
}
}
diff --git a/src/import.rs b/src/import.rs
index 89d7806..dc5a9fc 100644
--- a/src/import.rs
+++ b/src/import.rs
@@ -11,23 +11,19 @@ use std::io::Read;
use serde_json;
-use result::Result;
use task::Task;
-use error::{TaskError, TaskErrorKind};
+use error::{Result, ResultExt};
+use error::ErrorKind as EK;
/// Import taskwarrior-exported JSON. This expects an JSON Array of objects, as exported by
/// taskwarrior.
pub fn import<R: Read>(r: R) -> Result<Vec<Task>> {
- serde_json::from_reader(r).map_err(|e| {
- TaskError::new(TaskErrorKind::ParserError, Some(Box::new(e)))
- })
+ serde_json::from_reader(r).chain_err(|| EK::ParserError)
}
/// Import a single JSON-formatted Task
pub fn import_task(s: &str) -> Result<Task> {
- serde_json::from_str(s).map_err(|e| {
- TaskError::new(TaskErrorKind::ParserError, Some(Box::new(e)))
- })
+ serde_json::from_str(s).chain_err(|| EK::ParserError)
}
/// Reads line by line and tries to parse a task-object per line.
@@ -35,10 +31,7 @@ pub fn import_tasks<BR: BufRead>(r: BR) -> Vec<Result<Task>> {
let mut vt = Vec::new();
for line in r.lines() {
if line.is_err() {
- vt.push(Err(TaskError::new(
- TaskErrorKind::ReaderError,
- Some(Box::new(line.unwrap_err())),
- )));
+ vt.push(Err(line.unwrap_err()).chain_err(|| EK::ReaderError));
continue;
}
// Unwrap is safe because of continue above
diff --git a/src/lib.rs b/src/lib.rs
index 10396ba..3eeb8be 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -50,7 +50,10 @@ extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate uuid;
-#[macro_use] extern crate derive_builder;
+#[macro_use]
+extern crate derive_builder;
+#[macro_use]
+extern crate error_chain;
#[cfg(test)]
extern crate env_logger;
@@ -63,7 +66,6 @@ pub mod error;
pub mod import;
pub mod priority;
pub mod project;
-pub mod result;
pub mod status;
pub mod tag;
pub mod task;
diff --git a/src/result.rs b/src/result.rs
deleted file mode 100644
index 970c0f7..0000000
--- a/src/result.rs
+++ /dev/null
@@ -1,14 +0,0 @@
-//
-// This Source Code Form is subject to the terms of the Mozilla Public
-// License, v. 2.0. If a copy of the MPL was not distributed with this
-// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-//
-
-//! Module containing `Result` type
-
-use std::result::Result as RResult;
-
-use error::TaskError;
-
-/// Result type wrapping the standard std::result::Result type
-pub type Result<T> = RResult<T, TaskError>;