Add more context in error messages

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-05-18 00:14:34 +02:00
parent 3321b9a643
commit 26de159415
2 changed files with 27 additions and 16 deletions

View file

@ -57,12 +57,17 @@ impl Mail for Entry {
.context(format_err!("Cannot parse Email {}", mail_file_location.display()))?
.headers
.into_iter()
.filter_map(|hdr| match hdr.get_key() {
Err(e) => Some(Err(e).map_err(Error::from)),
.filter_map(|hdr| {
match hdr.get_key()
.context(format_err!("Cannot fetch key '{}' from Email {}", field, mail_file_location.display()))
.map_err(Error::from)
{
Ok(k) => if k == field {
Some(Ok(hdr))
} else {
None
},
Err(e) => Some(Err(e)),
}
})
.next()
@ -135,12 +140,17 @@ impl<'a> MailHeader<'a> {
pub fn get_field(&self, field: &str) -> Result<Option<String>> {
match self.0
.iter()
.filter_map(|hdr| match hdr.get_key() {
Err(e) => Some(Err(e).map_err(Error::from)),
.filter_map(|hdr| {
match hdr.get_key()
.context(format_err!("Cannot get field {}", field))
.map_err(Error::from)
{
Ok(key) => if key == field {
Some(Ok(hdr))
} else {
None
},
Err(e) => Some(Err(e))
}
})
.next()

View file

@ -39,8 +39,7 @@ pub(crate) fn get_message_header_at_key<P: AsRef<Path>, K: AsRef<str>>(p: P, k:
.context(format_err!("Cannot parse Email {}", p.as_ref().display()))?
.headers
.into_iter()
.filter_map(|hdr| match hdr.get_key() {
Err(e) => Some(Err(e).map_err(Error::from)),
.filter_map(|hdr| match hdr.get_key().context("Cannot get key from mail header").map_err(Error::from) {
Ok(key) => {
let lower_key = key.to_lowercase();
trace!("Test: {} == {}", lower_key, k.as_ref());
@ -49,11 +48,12 @@ pub(crate) fn get_message_header_at_key<P: AsRef<Path>, K: AsRef<str>>(p: P, k:
} else {
None
}
}
},
Err(e) => Some(Err(e))
})
.next()
.ok_or_else(|| format_err!("'{}' not found in {}", k.as_ref(), p.as_ref().display()))?
.and_then(|hdr| hdr.get_value().map_err(Error::from))
.and_then(|hdr| hdr.get_value().context("Cannot get value from mail header").map_err(Error::from))
}
pub(crate) fn get_message_id_for_mailfile<P: AsRef<Path>>(p: P) -> Result<String> {
@ -77,6 +77,7 @@ pub fn get_mail_text_content<P: AsRef<Path>>(p: P) -> Result<String> {
::mailparse::parse_mail(::std::fs::read_to_string(p.as_ref())?.as_bytes())
.context(format_err!("Cannot parse Email {}", p.as_ref().display()))?
.get_body()
.context("Cannot get body of mail")
.map_err(Error::from)
}