libimagmail: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 16:19:44 +02:00
parent 677a5e8886
commit 22cff91653
4 changed files with 36 additions and 27 deletions

View File

@ -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)
}
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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()