diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-09-03 16:19:44 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-09-03 21:33:54 +0200 |
commit | 22cff9165334bf56766f4953312426344fd0661b (patch) | |
tree | 7b147ec31c47db20c2d47e6460ab8d683bb4fdfe /lib/domain/libimagmail | |
parent | 677a5e88864698125264f635982a5b19351fe918 (diff) |
libimagmail: Rewrite error handling
Diffstat (limited to 'lib/domain/libimagmail')
-rw-r--r-- | lib/domain/libimagmail/src/error.rs | 25 | ||||
-rw-r--r-- | lib/domain/libimagmail/src/hasher.rs | 14 | ||||
-rw-r--r-- | lib/domain/libimagmail/src/lib.rs | 2 | ||||
-rw-r--r-- | lib/domain/libimagmail/src/mail.rs | 22 |
4 files changed, 36 insertions, 27 deletions
diff --git a/lib/domain/libimagmail/src/error.rs b/lib/domain/libimagmail/src/error.rs index 65eb2b31..b3a1568c 100644 --- a/lib/domain/libimagmail/src/error.rs +++ b/lib/domain/libimagmail/src/error.rs @@ -17,17 +17,36 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::error::Error; + +use libimagerror::into::IntoError; + error_chain! { types { MailError, MailErrorKind, ResultExt, Result; } + links { + RefError(::libimagentryref::error::RefError, ::libimagentryref::error::RefErrorKind); + } + + errors { RefCreationError { description("Error creating a reference to a file/directory") display("Error creating a reference to a file/directory") } + RefHandlingError { + description("Error handling a reference") + display("Error handling a reference") + } + + MailParsingError { + description("Failed to parse mail") + display("Failed to parse mail") + } + FetchByHashError { description("Error fetching mail from Store by hash") display("Error fetching mail from Store by hash") @@ -43,10 +62,6 @@ error_chain! { } } -pub use self::error::MailError; -pub use self::error::MailErrorKind; -pub use self::error::MapErrInto; - impl IntoError for MailErrorKind { type Target = MailError; @@ -54,7 +69,7 @@ impl IntoError for MailErrorKind { MailError::from_kind(self) } - fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target { + fn into_error_with_cause(self, _: Box<Error>) -> Self::Target { MailError::from_kind(self) } } diff --git a/lib/domain/libimagmail/src/hasher.rs b/lib/domain/libimagmail/src/hasher.rs index cc68d9a8..d650bedd 100644 --- a/lib/domain/libimagmail/src/hasher.rs +++ b/lib/domain/libimagmail/src/hasher.rs @@ -25,11 +25,8 @@ use email::MimeMessage; use libimagentryref::hasher::Hasher; use libimagentryref::hasher::DefaultHasher; use libimagentryref::error::RefErrorKind as REK; -use libimagentryref::error::MapErrInto; +use libimagentryref::error::ResultExt; use libimagentryref::result::Result as RResult; -use libimagerror::into::IntoError; - -use error::MailErrorKind as MEK; pub struct MailHasher { defaulthasher: DefaultHasher, @@ -54,12 +51,10 @@ impl Hasher for MailHasher { use email::Header; let mut s = String::new(); - try!(c.read_to_string(&mut s).map_err_into(REK::UTF8Error).map_err_into(REK::IOError)); + try!(c.read_to_string(&mut s).chain_err(|| REK::UTF8Error).chain_err(|| REK::IOError)); MimeMessage::parse(&s) - .map_err(Box::new) - .map_err(|e| MEK::MailParsingError.into_error_with_cause(e)) - .map_err_into(REK::RefHashingError) + .chain_err(|| REK::RefHashingError) .and_then(|mail| { let has_key = |hdr: &Header, exp: &str| hdr.name == exp; @@ -73,8 +68,7 @@ impl Hasher for MailHasher { for hdr in mail.headers.iter().filter(|item| filter.filter(item)) { let s = try!(hdr .get_value() - .map_err(Box::new) - .map_err(|e| REK::RefHashingError.into_error_with_cause(e))); + .chain_err(|| REK::RefHashingError)); v.push(s); } diff --git a/lib/domain/libimagmail/src/lib.rs b/lib/domain/libimagmail/src/lib.rs index 5fd42347..09e74a5f 100644 --- a/lib/domain/libimagmail/src/lib.rs +++ b/lib/domain/libimagmail/src/lib.rs @@ -40,7 +40,7 @@ extern crate email; extern crate filters; #[macro_use] extern crate error_chain; -#[macro_use] extern crate libimagerror; +extern crate libimagerror; extern crate libimagstore; extern crate libimagentryref; diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index ede27704..17129c90 100644 --- a/lib/domain/libimagmail/src/mail.rs +++ b/lib/domain/libimagmail/src/mail.rs @@ -31,7 +31,7 @@ use email::results::ParsingResult as EmailParsingResult; use hasher::MailHasher; use result::Result; -use error::{MapErrInto, MailErrorKind as MEK}; +use error::{ResultExt, MailErrorKind as MEK}; struct Buffer(String); @@ -59,17 +59,17 @@ impl<'a> Mail<'a> { let p = PathBuf::from(p.as_ref()); Ref::create_with_hasher(store, p, f, h) - .map_err_into(MEK::RefCreationError) + .chain_err(|| MEK::RefCreationError) .and_then(|reference| { debug!("Build reference file: {:?}", reference); reference.fs_file() - .map_err_into(MEK::RefHandlingError) - .and_then(|path| File::open(path).map_err_into(MEK::IOError)) + .chain_err(|| MEK::RefHandlingError) + .and_then(|path| File::open(path).chain_err(|| MEK::IOError)) .and_then(|mut file| { let mut s = String::new(); file.read_to_string(&mut s) .map(|_| s) - .map_err_into(MEK::IOError) + .chain_err(|| MEK::IOError) }) .map(Buffer::from) .map(|buffer| Mail(reference, buffer)) @@ -80,8 +80,8 @@ impl<'a> Mail<'a> { pub fn open<S: AsRef<str>>(store: &Store, hash: S) -> Result<Option<Mail>> { debug!("Opening Mail by Hash"); Ref::get_by_hash(store, String::from(hash.as_ref())) - .map_err_into(MEK::FetchByHashError) - .map_err_into(MEK::FetchError) + .chain_err(|| MEK::FetchByHashError) + .chain_err(|| MEK::FetchError) .and_then(|o| match o { Some(r) => Mail::from_ref(r).map(Some), None => Ok(None), @@ -93,13 +93,13 @@ impl<'a> Mail<'a> { pub fn from_ref(r: Ref<'a>) -> Result<Mail> { debug!("Building Mail object from Ref: {:?}", r); r.fs_file() - .map_err_into(MEK::RefHandlingError) - .and_then(|path| File::open(path).map_err_into(MEK::IOError)) + .chain_err(|| MEK::RefHandlingError) + .and_then(|path| File::open(path).chain_err(|| MEK::IOError)) .and_then(|mut file| { let mut s = String::new(); file.read_to_string(&mut s) .map(|_| s) - .map_err_into(MEK::IOError) + .chain_err(|| MEK::IOError) }) .map(Buffer::from) .map(|buffer| Mail(r, buffer)) @@ -109,7 +109,7 @@ impl<'a> Mail<'a> { debug!("Getting field in mail: {:?}", field); self.1 .parsed() - .map_err_into(MEK::MailParsingError) + .chain_err(|| MEK::MailParsingError) .map(|parsed| { parsed.headers .iter() |