libimagmail: Rewrite error handling
This commit is contained in:
parent
677a5e8886
commit
22cff91653
4 changed files with 36 additions and 27 deletions
|
@ -17,17 +17,36 @@
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
//
|
//
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
error_chain! {
|
error_chain! {
|
||||||
types {
|
types {
|
||||||
MailError, MailErrorKind, ResultExt, Result;
|
MailError, MailErrorKind, ResultExt, Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
links {
|
||||||
|
RefError(::libimagentryref::error::RefError, ::libimagentryref::error::RefErrorKind);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
errors {
|
errors {
|
||||||
RefCreationError {
|
RefCreationError {
|
||||||
description("Error creating a reference to a file/directory")
|
description("Error creating a reference to a file/directory")
|
||||||
display("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 {
|
FetchByHashError {
|
||||||
description("Error fetching mail from Store by hash")
|
description("Error fetching mail from Store by hash")
|
||||||
display("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 {
|
impl IntoError for MailErrorKind {
|
||||||
type Target = MailError;
|
type Target = MailError;
|
||||||
|
|
||||||
|
@ -54,7 +69,7 @@ impl IntoError for MailErrorKind {
|
||||||
MailError::from_kind(self)
|
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)
|
MailError::from_kind(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,11 +25,8 @@ use email::MimeMessage;
|
||||||
use libimagentryref::hasher::Hasher;
|
use libimagentryref::hasher::Hasher;
|
||||||
use libimagentryref::hasher::DefaultHasher;
|
use libimagentryref::hasher::DefaultHasher;
|
||||||
use libimagentryref::error::RefErrorKind as REK;
|
use libimagentryref::error::RefErrorKind as REK;
|
||||||
use libimagentryref::error::MapErrInto;
|
use libimagentryref::error::ResultExt;
|
||||||
use libimagentryref::result::Result as RResult;
|
use libimagentryref::result::Result as RResult;
|
||||||
use libimagerror::into::IntoError;
|
|
||||||
|
|
||||||
use error::MailErrorKind as MEK;
|
|
||||||
|
|
||||||
pub struct MailHasher {
|
pub struct MailHasher {
|
||||||
defaulthasher: DefaultHasher,
|
defaulthasher: DefaultHasher,
|
||||||
|
@ -54,12 +51,10 @@ impl Hasher for MailHasher {
|
||||||
use email::Header;
|
use email::Header;
|
||||||
|
|
||||||
let mut s = String::new();
|
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)
|
MimeMessage::parse(&s)
|
||||||
.map_err(Box::new)
|
.chain_err(|| REK::RefHashingError)
|
||||||
.map_err(|e| MEK::MailParsingError.into_error_with_cause(e))
|
|
||||||
.map_err_into(REK::RefHashingError)
|
|
||||||
.and_then(|mail| {
|
.and_then(|mail| {
|
||||||
let has_key = |hdr: &Header, exp: &str| hdr.name == exp;
|
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)) {
|
for hdr in mail.headers.iter().filter(|item| filter.filter(item)) {
|
||||||
let s = try!(hdr
|
let s = try!(hdr
|
||||||
.get_value()
|
.get_value()
|
||||||
.map_err(Box::new)
|
.chain_err(|| REK::RefHashingError));
|
||||||
.map_err(|e| REK::RefHashingError.into_error_with_cause(e)));
|
|
||||||
|
|
||||||
v.push(s);
|
v.push(s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ extern crate email;
|
||||||
extern crate filters;
|
extern crate filters;
|
||||||
#[macro_use] extern crate error_chain;
|
#[macro_use] extern crate error_chain;
|
||||||
|
|
||||||
#[macro_use] extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
extern crate libimagentryref;
|
extern crate libimagentryref;
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ use email::results::ParsingResult as EmailParsingResult;
|
||||||
|
|
||||||
use hasher::MailHasher;
|
use hasher::MailHasher;
|
||||||
use result::Result;
|
use result::Result;
|
||||||
use error::{MapErrInto, MailErrorKind as MEK};
|
use error::{ResultExt, MailErrorKind as MEK};
|
||||||
|
|
||||||
struct Buffer(String);
|
struct Buffer(String);
|
||||||
|
|
||||||
|
@ -59,17 +59,17 @@ impl<'a> Mail<'a> {
|
||||||
let p = PathBuf::from(p.as_ref());
|
let p = PathBuf::from(p.as_ref());
|
||||||
|
|
||||||
Ref::create_with_hasher(store, p, f, h)
|
Ref::create_with_hasher(store, p, f, h)
|
||||||
.map_err_into(MEK::RefCreationError)
|
.chain_err(|| MEK::RefCreationError)
|
||||||
.and_then(|reference| {
|
.and_then(|reference| {
|
||||||
debug!("Build reference file: {:?}", reference);
|
debug!("Build reference file: {:?}", reference);
|
||||||
reference.fs_file()
|
reference.fs_file()
|
||||||
.map_err_into(MEK::RefHandlingError)
|
.chain_err(|| MEK::RefHandlingError)
|
||||||
.and_then(|path| File::open(path).map_err_into(MEK::IOError))
|
.and_then(|path| File::open(path).chain_err(|| MEK::IOError))
|
||||||
.and_then(|mut file| {
|
.and_then(|mut file| {
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
file.read_to_string(&mut s)
|
file.read_to_string(&mut s)
|
||||||
.map(|_| s)
|
.map(|_| s)
|
||||||
.map_err_into(MEK::IOError)
|
.chain_err(|| MEK::IOError)
|
||||||
})
|
})
|
||||||
.map(Buffer::from)
|
.map(Buffer::from)
|
||||||
.map(|buffer| Mail(reference, buffer))
|
.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>> {
|
pub fn open<S: AsRef<str>>(store: &Store, hash: S) -> Result<Option<Mail>> {
|
||||||
debug!("Opening Mail by Hash");
|
debug!("Opening Mail by Hash");
|
||||||
Ref::get_by_hash(store, String::from(hash.as_ref()))
|
Ref::get_by_hash(store, String::from(hash.as_ref()))
|
||||||
.map_err_into(MEK::FetchByHashError)
|
.chain_err(|| MEK::FetchByHashError)
|
||||||
.map_err_into(MEK::FetchError)
|
.chain_err(|| MEK::FetchError)
|
||||||
.and_then(|o| match o {
|
.and_then(|o| match o {
|
||||||
Some(r) => Mail::from_ref(r).map(Some),
|
Some(r) => Mail::from_ref(r).map(Some),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
|
@ -93,13 +93,13 @@ impl<'a> Mail<'a> {
|
||||||
pub fn from_ref(r: Ref<'a>) -> Result<Mail> {
|
pub fn from_ref(r: Ref<'a>) -> Result<Mail> {
|
||||||
debug!("Building Mail object from Ref: {:?}", r);
|
debug!("Building Mail object from Ref: {:?}", r);
|
||||||
r.fs_file()
|
r.fs_file()
|
||||||
.map_err_into(MEK::RefHandlingError)
|
.chain_err(|| MEK::RefHandlingError)
|
||||||
.and_then(|path| File::open(path).map_err_into(MEK::IOError))
|
.and_then(|path| File::open(path).chain_err(|| MEK::IOError))
|
||||||
.and_then(|mut file| {
|
.and_then(|mut file| {
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
file.read_to_string(&mut s)
|
file.read_to_string(&mut s)
|
||||||
.map(|_| s)
|
.map(|_| s)
|
||||||
.map_err_into(MEK::IOError)
|
.chain_err(|| MEK::IOError)
|
||||||
})
|
})
|
||||||
.map(Buffer::from)
|
.map(Buffer::from)
|
||||||
.map(|buffer| Mail(r, buffer))
|
.map(|buffer| Mail(r, buffer))
|
||||||
|
@ -109,7 +109,7 @@ impl<'a> Mail<'a> {
|
||||||
debug!("Getting field in mail: {:?}", field);
|
debug!("Getting field in mail: {:?}", field);
|
||||||
self.1
|
self.1
|
||||||
.parsed()
|
.parsed()
|
||||||
.map_err_into(MEK::MailParsingError)
|
.chain_err(|| MEK::MailParsingError)
|
||||||
.map(|parsed| {
|
.map(|parsed| {
|
||||||
parsed.headers
|
parsed.headers
|
||||||
.iter()
|
.iter()
|
||||||
|
|
Loading…
Reference in a new issue